Recipe: build a verifiable Worker JSON API
Produce a first edge API with a minimal file tree and explicit success and error responses.
Edited and verified by Orange Book Editorial Team ·
Outcome preview
GET /api/health returns JSON with a request ID; every other path returns a structured 404. You will verify both branches.
What you need
- Complete deploy your first Worker.
- Make sure the local project can run
wrangler dev.
File tree
Implement and verify
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
const requestId = crypto.randomUUID();
if (request.method === 'GET' && url.pathname === '/api/health') {
return Response.json(
{ ok: true, requestId },
{ headers: { 'cache-control': 'no-store' } },
);
}
return Response.json(
{ ok: false, error: 'not_found', requestId },
{ status: 404 },
);
},
};Why the health response is not cached
This recipe uses cache-control: no-store to state that health data should be generated live. A later static-content recipe will make a different choice.
AI review prompt
Checkpoint and pitfalls
- Every response should contain a new
requestId. - A
404is an expected branch, not a Worker crash. - Do not put API tokens, account IDs, or secrets in responses or the repository.
- Before deployment, read the current
wrangler deploydocumentation and validate in your own preview environment.
Next: understand the API architecture boundary.
Primary sources
Did this page help you complete your goal?
Beta feedback is generated in this browser and is never uploaded automatically.
Recipe: Cache Rules that do not break sign-in or APIs
Bypass identity, account, and write paths first, cache only public content, and verify the policy with headers and separate clients.
Architecture: ownership along one request
Understand the Cloudflare request lifecycle through responsibility boundaries, data flow, and failure ownership.