add-api-route

Creates Astro API routes with Cloudflare D1 access, authentication, and Zod validation.

Updated Mar 28, 2026
One-click install
npx skills add https://github.com/po4yka/blog --skill add-api-route-po4yka
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: add-api-route
Source: https://github.com/po4yka/blog/tree/main/.agents/skills/add-api-route
Command: npx skills add https://github.com/po4yka/blog --skill add-api-route-po4yka

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Adding a new server endpoint in an Astro + Cloudflare D1 project requires following several non-obvious conventions—disabling prerendering, authenticating via a thrown Response, accessing D1 through the runtime adapter, and validating input with Zod. This Skill encodes those patterns so new API routes are created correctly the first time. ## Core Features & Use Cases - Route Templates: Provides ready-to-adapt templates for collection endpoints (GET/POST on index.ts) and item endpoints (GET/PUT/DELETE on [id].ts) under src/pages/api/. - Auth and Validation Patterns: Enforces requireAuth before data access, getDb(locals.runtime.env) for D1 bindings, and Zod safeParse with validationError for request bodies. - Use Case: When adding a new admin endpoint such as /api/admin/posts, use this Skill to generate the route file with export const prerender = false, Bearer-token auth, D1 queries, and typed Zod validation already wired in. ## Quick Start Create a new authenticated admin API route for the projects entity with GET and POST handlers following the project's D1 and Zod patterns.

Frequently Asked Questions about add-api-route

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create an API route in Astro with Cloudflare D1?

Place a TypeScript file under src/pages/api/, export `prerender = false`, and export named HTTP method handlers like GET or POST typed as APIRoute. Access D1 through getDb(locals.runtime.env), which reads the DB binding injected by the @astrojs/cloudflare adapter.

How to add authentication to an Astro API endpoint?

Call await requireAuth(request, db) before any data access. It validates the Bearer token and throws a Response object on failure, which Astro sends directly to the client as a 401. Omit it only for public endpoints like login.

Why does my Astro API route fail during the build?

The route is missing `export const prerender = false`. Without it, Astro treats the file as a static route and evaluates it at build time, where locals.runtime.env and the D1 binding are unavailable, causing build errors.

How do I validate request bodies in Astro API routes?

Define a schema in src/lib/validation.ts using z from astro/zod, then call schema.safeParse(await request.json()). On failure return validationError(parsed.error), which produces a 400 response with issue details; on success parsed.data is fully typed.

How do dynamic route parameters work in Astro API endpoints?

Name the file [id].ts and read the value from params.id inside the handler. The parameter is guaranteed by the route, so a non-null assertion like params.id! is safe to use when passing it to database functions.