Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/content/docs/en/guides/endpoints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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);

Expand All @@ -132,24 +133,26 @@ 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`.

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
Expand All @@ -159,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({
Expand Down Expand Up @@ -201,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();
Expand All @@ -224,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);

Expand All @@ -239,5 +247,5 @@ export async function GET({ params, redirect }) {
}

return redirect(link, 307);
}
}) satisfies APIRoute;
```
Loading