Agent skill
new-api-route
Creates a new Cloudflare Workers API route with typed context, JSON validation, and error handling. Use when adding a new API endpoint or server-side handler.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/new-api-route
SKILL.md
Create a new API route in the src/pages/api/ directory.
Steps
- Take the route path from the user's argument (e.g., "webhook", "media/upload"). If not provided, ask for one.
- Determine the file path:
src/pages/api/<route-path>.ts. Use kebab-case for filenames. - Ask the user:
- What HTTP method(s) should it handle? (POST is most common)
- What fields does the request body contain?
- Should it require Cloudflare Access authentication?
- Create the API route file using the project template:
import type { APIContext } from "astro";
export async function POST(context: APIContext) {
const env = context.locals.runtime.env;
let body: { /* typed fields */ };
try {
body = await context.request.json();
} catch {
return new Response(JSON.stringify({ error: "Invalid JSON" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
// Validate required fields
// Process request
// Return JSON response with Content-Type header
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
- If authentication is needed, add the auth guard at the top of the handler:
import { verifyAccessJwt } from "../../lib/access";
// Inside handler:
const user = await verifyAccessJwt(context.request, env);
if (!user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 403,
headers: { "Content-Type": "application/json" },
});
}
-
Follow these conventions:
- Always wrap
context.request.json()in try/catch - Always set
Content-Type: application/jsonon responses - Access Cloudflare bindings via
context.locals.runtime.env - API routes are SSR by default (no prerender export)
- Type the request body explicitly
- Always wrap
-
Suggest writing a test for the new route using
createMockContext()fromtests/setup.ts.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?