From 3ddf29ec105a6afceb30575bceeb83d1662f9746 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 17 Jun 2026 18:14:47 +0200 Subject: [PATCH 1/5] id can be undefined and if infered TS complains because string type --- src/content/docs/en/guides/endpoints.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/en/guides/endpoints.mdx b/src/content/docs/en/guides/endpoints.mdx index b604d8501eb8b..9edf58c41dcf8 100644 --- a/src/content/docs/en/guides/endpoints.mdx +++ b/src/content/docs/en/guides/endpoints.mdx @@ -59,7 +59,7 @@ import type { APIRoute } from "astro"; const usernames = ["Sarah", "Chris", "Yan", "Elian"]; export const GET = (({ params, request }) => { - const id = params.id; + const id = Number(params.id); return new Response( JSON.stringify({ From 4bdb12a58ae7a96e333762823e06ce9ebe7b1d08 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 17 Jun 2026 18:18:33 +0200 Subject: [PATCH 2/5] TS complains about params without APIRoute --- src/content/docs/en/guides/endpoints.mdx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/content/docs/en/guides/endpoints.mdx b/src/content/docs/en/guides/endpoints.mdx index 9edf58c41dcf8..083ac486e63c8 100644 --- a/src/content/docs/en/guides/endpoints.mdx +++ b/src/content/docs/en/guides/endpoints.mdx @@ -113,9 +113,10 @@ Be sure to [enable an on-demand rendering mode](/en/guides/on-demand-rendering/) Server endpoints can access `params` without exporting `getStaticPaths`, and they can return a `Response` object, allowing you to set status codes and headers: ```js title="src/pages/[id].json.js" +import type { APIRoute } from "astro"; import { getProduct } from "../db"; -export async function GET({ params }) { +export const GET = (async ({ params }) => { const id = params.id; const product = await getProduct(id); @@ -132,7 +133,7 @@ export async function GET({ params }) { "Content-Type": "application/json", }, }); -} +}) satisfies APIRoute; ``` This will respond to any request that matches the dynamic route. For example, if we navigate to `/helmet.json`, `params.id` will be set to `helmet`. If `helmet` exists in the mock product database, the endpoint will use a `Response` object to respond with JSON and return a successful [HTTP status code](https://developer.mozilla.org/en-US/docs/Web/API/Response/status). If not, it will use a `Response` object to respond with a `404`. From 875f3662f6f956cd3000478fb4005d62854b0794 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 17 Jun 2026 18:22:32 +0200 Subject: [PATCH 3/5] APIRoute missing and TS complains without @types/node --- src/content/docs/en/guides/endpoints.mdx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/content/docs/en/guides/endpoints.mdx b/src/content/docs/en/guides/endpoints.mdx index 083ac486e63c8..31ffdcd66bf1a 100644 --- a/src/content/docs/en/guides/endpoints.mdx +++ b/src/content/docs/en/guides/endpoints.mdx @@ -141,16 +141,18 @@ This will respond to any request that matches the dynamic route. For example, if In SSR mode, certain providers require the `Content-Type` header to return an image. In this case, use a `Response` object to specify a `headers` property. For example, to produce a binary `.png` image: ```ts title="src/pages/astro-logo.png.ts" -export async function GET({ params, request }) { +import type { APIRoute } from "astro"; + +export const GET = (async ({ params, request }) => { const response = await fetch( "https://docs.astro.build/assets/full-logo-light.png", ); - const buffer = Buffer.from(await response.arrayBuffer()); - + const buffer = Buffer.from(await response.arrayBuffer()); // requires `@types/node` in your dependencies for type safety + return new Response(buffer, { headers: { "Content-Type": "image/png" }, }); -} +}) satisfies APIRoute; ``` ### HTTP methods From c359008d254992bc7404f46f80b9d0adae244820 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 17 Jun 2026 18:25:59 +0200 Subject: [PATCH 4/5] missing import --- src/content/docs/en/guides/endpoints.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/content/docs/en/guides/endpoints.mdx b/src/content/docs/en/guides/endpoints.mdx index 31ffdcd66bf1a..3bd6fe3fbcb0c 100644 --- a/src/content/docs/en/guides/endpoints.mdx +++ b/src/content/docs/en/guides/endpoints.mdx @@ -162,6 +162,8 @@ In addition to the `GET` function, you can export a function with the name of an You can also export an `ALL` function to match any method that doesn't have a corresponding exported function. If there is a request with no matching method, it will redirect to your site's [404 page](/en/basics/astro-pages/#custom-404-error-page). ```ts title="src/pages/methods.json.ts" +import type { APIRoute } from "astro"; + export const GET = (({ params, request }) => { return new Response( JSON.stringify({ @@ -204,6 +206,8 @@ If you define a `GET` function but no `HEAD` function, Astro will automatically In SSR mode, the `request` property returns a fully usable [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object that refers to the current request. This allows you to accept data and check headers: ```ts title="src/pages/test-post.json.ts" +import type { APIRoute } from "astro"; + export const POST = (async ({ request }) => { if (request.headers.get("Content-Type") === "application/json") { const body = await request.json(); From 6a284462e45cfbbb49e4f7e197cb5539505a80eb Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 17 Jun 2026 18:27:29 +0200 Subject: [PATCH 5/5] missing APIRoute --- src/content/docs/en/guides/endpoints.mdx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/content/docs/en/guides/endpoints.mdx b/src/content/docs/en/guides/endpoints.mdx index 3bd6fe3fbcb0c..1292ae44cc3a9 100644 --- a/src/content/docs/en/guides/endpoints.mdx +++ b/src/content/docs/en/guides/endpoints.mdx @@ -231,10 +231,11 @@ export const POST = (async ({ request }) => { The endpoint context exports a `redirect()` utility similar to `Astro.redirect`: -```js title="src/pages/links/[id].js" {14} +```js title="src/pages/links/[id].js" {15} +import type { APIRoute } from "astro"; import { getLinkUrl } from "../db"; -export async function GET({ params, redirect }) { +export const GET = (async ({ params, redirect }) => { const { id } = params; const link = await getLinkUrl(id); @@ -246,5 +247,5 @@ export async function GET({ params, redirect }) { } return redirect(link, 307); -} +}) satisfies APIRoute; ```