The address (usually IP address) of the user.
Throws an error if used within a static site, or within a prerendered page.
Utility for getting and setting the values of cookies.
The current locale computed from the URL of the request. It matches the locales in i18n.locales, and returns undefined otherwise.
A human-readable string representing the Astro version used to create the project.
For example, "Astro v1.1.1".
Get action result on the server when using a form POST.
An object that middlewares can use to store extra information related to the request.
It will be made available to pages as Astro.locals, and to endpoints as context.locals.
Example usage:
// src/middleware.ts
import { defineMiddleware } from "astro:middleware";
export const onRequest = defineMiddleware((context, next) => {
context.locals.greeting = "Hello!";
return next();
});
.astro file:
```astroParameters matching the page’s dynamic route pattern.
In static builds, this will be the params generated by getStaticPaths.
In SSR builds, this can be any path segments matching the dynamic route pattern.
Example usage:
import type { APIContext } from "astro"
export function getStaticPaths() {
return [
{ params: { id: '0' }, props: { name: 'Sarah' } },
{ params: { id: '1' }, props: { name: 'Chris' } },
{ params: { id: '2' }, props: { name: 'Fuzzy' } },
];
}
export async function GET({ params }: APIContext) {
return new Response(`Hello user ${params.id}!`)
}
Available only when i18n configured and in SSR.
It represents the preferred locale of the user. It's computed by checking the supported locales in i18n.locales
and locales supported by the users's browser via the header Accept-Language
For example, given i18n.locales equals to ['fr', 'de'], and the Accept-Language value equals to en, de;q=0.2, fr;q=0.6, the
Astro.preferredLanguage will be fr because en is not supported, its quality value is the highest.
Available only when i18n configured and in SSR.
It represents the list of the preferred locales that are supported by the application. The list is sorted via quality value.
For example, given i18n.locales equals to ['fr', 'pt', 'de'], and the Accept-Language value equals to en, de;q=0.2, fr;q=0.6, the
Astro.preferredLocaleList will be equal to ['fs', 'de'] because en isn't supported, and pt isn't part of the locales contained in the
header.
When the Accept-Header is *, the original i18n.locales are returned. The value * means no preferences, so Astro returns all the supported locales.
List of props passed from getStaticPaths. Only available to static builds.
Example usage:
import type { APIContext } from "astro"
export function getStaticPaths() {
return [
{ params: { id: '0' }, props: { name: 'Sarah' } },
{ params: { id: '1' }, props: { name: 'Chris' } },
{ params: { id: '2' }, props: { name: 'Fuzzy' } },
];
}
export function GET({ props }: APIContext): Response {
return new Response(`Hello ${props.name}!`);
}
Create a response that redirects to another page.
Example usage:
// src/pages/secret.ts
export function GET({ redirect }) {
return redirect('/login');
}
Optional status: ValidRedirectStatusInformation about the current request. This is a standard Request object
It reroutes to another page. As opposed to redirects, the URL won't change, and Astro will render the HTML emitted by the rerouted URL passed as argument.
// src/pages/secret.ts
export function GET(ctx) {
return ctx.rewrite(new URL("../"), ctx.url);
}
The site provided in the astro config, parsed as an instance of URL, without base.
undefined if the site is not provided in the config.
The url of the current request, parsed as an instance of URL.
Equivalent to:
new URL(context.request.url)
The
APIContextis the object made available to endpoints and middleware. It is a subset of theAstroglobal object available in pages.Reference