Create Endpoints in Astro
From the Astro cheat sheet · API Routes & Endpoints · verified Jul 2026
Create Endpoints
typescript
// pages/api/posts.json.ts
import type { APIRoute } from 'astro';
export const GET: APIRoute = async ({ params, request }) => {
const posts = [
{ id: 1, title: 'Post 1' },
{ id: 2, title: 'Post 2' },
];
return new Response(JSON.stringify(posts), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};
export const POST: APIRoute = async ({ request }) => {
const data = await request.json();
return new Response(JSON.stringify({ success: true, data }), {
status: 201,
headers: { 'Content-Type': 'application/json' }
});
};✅ Export GET, POST, PUT, DELETE functions from .ts/.js files
💡 File extension determines output: .json.ts → .json
🔍 Access params, request, redirect from context object
⚡ SSR mode: live routes. Static mode: pre-rendered files
apiendpointsroutesAPIRoute