Skip to content

feat: add REST API for programmatic resource registration#744

Open
Manishcs076 wants to merge 1 commit intoMerit-Systems:mainfrom
Manishcs076:feat/programmatic-resource-api
Open

feat: add REST API for programmatic resource registration#744
Manishcs076 wants to merge 1 commit intoMerit-Systems:mainfrom
Manishcs076:feat/programmatic-resource-api

Conversation

@Manishcs076
Copy link
Copy Markdown

Summary

Exposes the existing internal tRPC register functionality as a public REST API endpoint.

Fixes #104

Endpoint

POST /api/resources/register

Single resource:

{ "url": "https://example.com/api/resource" }

Batch registration (up to 20):

{ "urls": ["https://example.com/api/a", "https://example.com/api/b"] }

Behavior

  • Probes each URL for x402 payment response before registering
  • Reuses existing registerResource() and probeX402Endpoint() logic
  • Returns discovery info for additional resources at the same origin
  • Proper error responses: 400 (validation), 422 (no 402/parse errors), 500 (internal)

Changes

  • New file: apps/scan/src/app/api/resources/register/route.ts (215 lines)
  • No changes to existing code

Exposes the existing tRPC register functionality as a public REST
endpoint at POST /api/resources/register.

Supports:
- Single resource: { "url": "https://..." }
- Batch registration: { "urls": ["https://...", ...] } (max 20)
- Probes each URL for x402 payment response before registering
- Returns discovery info for additional resources at the same origin
- Proper error responses (400 validation, 422 no 402/parse errors, 500 internal)

Fixes Merit-Systems#104
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 27, 2026

@Manishcs076 is attempting to deploy a commit to the Merit Systems Team on Vercel.

A member of the Team first needs to authorize it.

},
},
{ status: 500 }
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing validation error handling for invalid JSON in POST request body causes SyntaxError to return 500 instead of 400

Fix on Vercel

Comment on lines +209 to +214
return NextResponse.json({
success: true,
registered: registered.length,
failed: failed.length,
results,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return NextResponse.json({
success: true,
registered: registered.length,
failed: failed.length,
results,
});
// Determine success: at least one URL must be successfully registered
const hasAnySuccess = registered.length > 0;
const status = hasAnySuccess ? 200 : 422;
return NextResponse.json(
{
success: hasAnySuccess,
registered: registered.length,
failed: failed.length,
results,
},
{ status }
);

Batch registration always returns success: true even when all URLs fail to register, with HTTP 200 status code

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add Resources Programmatically

1 participant