Pages and routing
Create a static endpoint
Return JSON, text, XML, or another response from a .js or .ts route file.
8 minute lesson
An endpoint returns a Response instead of page HTML.
export function GET() {
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' }
})
}
Place this in a route such as src/pages/api/status.ts. In a static build, Astro runs the GET handler during the build and writes the response body to a file.
That static response cannot change per visitor. It also cannot handle a POST after deployment.
An on-demand endpoint runs for an incoming request. It can read request headers, validate input, and return current data, but it requires a compatible adapter and server runtime.
Return accurate headers and status codes. JSON needs a JSON content type. A missing record should not return a successful status with an error string.
Treat route parameters, headers, and request bodies as untrusted. Being inside an Astro endpoint does not provide authentication or validation automatically.
Build the static endpoint and open its output. Then change a value and notice it stays stale until the next build.
Lesson completed