> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/withastro/astro/llms.txt
> Use this file to discover all available pages before exploring further.

# Astro Global Object

> API reference for the Astro global object available in .astro files

The `Astro` global object is available in all `.astro` components and provides access to request data, routing information, and utilities for rendering.

## Properties

<ParamField path="Astro.props" type="Record<string, any>">
  Component props passed from parent components or from `getStaticPaths()`.

  ```astro theme={null}
  ---
  // MyComponent.astro
  const { title, description } = Astro.props;
  ---
  <h1>{title}</h1>
  <p>{description}</p>
  ```
</ParamField>

<ParamField path="Astro.request" type="Request">
  A standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object containing information about the current request.

  ```astro theme={null}
  ---
  const userAgent = Astro.request.headers.get('user-agent');
  const method = Astro.request.method;
  ---
  ```
</ParamField>

<ParamField path="Astro.url" type="URL">
  A URL object constructed from the current request URL. Equivalent to `new URL(Astro.request.url)`.

  ```astro theme={null}
  ---
  const pathname = Astro.url.pathname;
  const searchParams = Astro.url.searchParams;
  const origin = Astro.url.origin;
  ---
  <p>Current path: {pathname}</p>
  ```
</ParamField>

<ParamField path="Astro.params" type="Record<string, string | undefined>">
  An object containing the values of dynamic route segments matched for this request.

  ```astro theme={null}
  ---
  // src/pages/blog/[slug].astro
  const { slug } = Astro.params;
  ---
  <h1>Post: {slug}</h1>
  ```
</ParamField>

<ParamField path="Astro.site" type="URL | undefined">
  The `site` from your Astro config, parsed as a URL instance. Returns `undefined` if not set.

  ```astro theme={null}
  ---
  const siteUrl = Astro.site;
  ---
  <link rel="canonical" href={new URL(Astro.url.pathname, Astro.site)} />
  ```
</ParamField>

<ParamField path="Astro.generator" type="string">
  A string representing the Astro version, in the format `"Astro v5.x.x"`.

  ```astro theme={null}
  <meta name="generator" content={Astro.generator} />
  ```
</ParamField>

<ParamField path="Astro.cookies" type="AstroCookies">
  Utilities for reading and manipulating cookies in on-demand routes.

  ```astro theme={null}
  ---
  const sessionId = Astro.cookies.get('session');
  Astro.cookies.set('theme', 'dark', {
    path: '/',
    maxAge: 60 * 60 * 24 * 7 // 1 week
  });
  ---
  ```
</ParamField>

<ParamField path="Astro.locals" type="App.Locals">
  An object that middleware can use to store extra information related to the request.

  ```astro theme={null}
  ---
  // Access value set in middleware
  const user = Astro.locals.user;
  ---
  <p>Welcome, {user.name}!</p>
  ```
</ParamField>

<ParamField path="Astro.clientAddress" type="string">
  The IP address of the client making the request. Only available in on-demand routes.

  ```astro theme={null}
  ---
  const ip = Astro.clientAddress;
  ---
  ```
</ParamField>

<ParamField path="Astro.response" type="ResponseInit & { readonly headers: Headers }">
  A standard ResponseInit object for modifying the outgoing response.

  ```astro theme={null}
  ---
  Astro.response.status = 404;
  Astro.response.headers.set('X-Custom-Header', 'value');
  ---
  ```
</ParamField>

<ParamField path="Astro.self" type="AstroComponentFactory">
  Allows a component to recursively call itself.

  ```astro theme={null}
  ---
  const { items } = Astro.props;
  ---
  <ul>
    {items.map((item) => (
      <li>
        {Array.isArray(item) ? (
          <Astro.self items={item} />
        ) : (
          item
        )}
      </li>
    ))}
  </ul>
  ```
</ParamField>

<ParamField path="Astro.slots" type="object">
  An object containing utility functions for working with slotted children.

  ### Methods

  <ParamField path="Astro.slots.has" type="(slotName: string) => boolean">
    Check whether content for a slot name exists.

    ```astro theme={null}
    ---
    const hasHeader = Astro.slots.has('header');
    ---
    {hasHeader && <header><slot name="header" /></header>}
    <slot />
    ```
  </ParamField>

  <ParamField path="Astro.slots.render" type="(slotName: string, args?: any[]) => Promise<string>">
    Asynchronously renders the contents of a slot to a string of HTML.

    ```astro theme={null}
    ---
    let html = '';
    if (Astro.slots.has('default')) {
      html = await Astro.slots.render('default');
    }
    ---
    <Fragment set:html={html} />
    ```
  </ParamField>
</ParamField>

<ParamField path="Astro.currentLocale" type="string | undefined">
  The current locale computed from the URL of the request. Only available when i18n routing is configured.

  ```astro theme={null}
  ---
  const locale = Astro.currentLocale; // 'en', 'fr', etc.
  ---
  ```
</ParamField>

<ParamField path="Astro.preferredLocale" type="string | undefined">
  The best match between visitor's browser language preferences and supported locales. Only available in on-demand routes.

  ```astro theme={null}
  ---
  const preferred = Astro.preferredLocale;
  ---
  ```
</ParamField>

<ParamField path="Astro.preferredLocaleList" type="string[] | undefined">
  List of all locales that are both requested by the browser and supported by the site. Only available in on-demand routes.

  ```astro theme={null}
  ---
  const locales = Astro.preferredLocaleList;
  ---
  ```
</ParamField>

<ParamField path="Astro.isPrerendered" type="boolean">
  Whether the current route is prerendered or not.

  ```astro theme={null}
  ---
  if (!Astro.isPrerendered) {
    // Use on-demand features
  }
  ---
  ```
</ParamField>

<ParamField path="Astro.routePattern" type="string">
  The route pattern for the current route, stripped of the `srcDir` and `pages` folder.

  ```astro theme={null}
  ---
  // src/pages/index.astro -> '/'
  // src/pages/blog/[slug].astro -> '/blog/[slug]'
  const pattern = Astro.routePattern;
  ---
  ```
</ParamField>

<ParamField path="Astro.originPathname" type="string">
  The original pathname before any rewrites were applied. Useful for tracking the original URL.

  ```astro theme={null}
  ---
  const original = Astro.originPathname;
  const current = Astro.url.pathname;
  ---
  ```
</ParamField>

## Methods

<ParamField path="Astro.redirect" type="(path: string, status?: number) => Response">
  Create a response that redirects to another page.

  ```astro theme={null}
  ---
  import type { APIContext } from 'astro';

  export function GET({ redirect }: APIContext) {
    return redirect('/login', 302);
  }
  ---
  ```
</ParamField>

<ParamField path="Astro.rewrite" type="(rewritePayload: RewritePayload) => Promise<Response>">
  Serve content from a different URL or path without redirecting the browser.

  ```astro theme={null}
  ---
  export async function GET(context) {
    return context.rewrite('/new-page');
  }
  ---
  ```
</ParamField>

<ParamField path="Astro.callAction" type="<TAction>(action: TAction, input: Parameters<TAction>[0]) => Promise<ActionReturnType<TAction>>">
  Call an Action handler directly from your Astro component.

  ```astro theme={null}
  ---
  import { actions } from 'astro:actions';

  const result = await Astro.callAction(actions.getPost, { postId: 'test' });
  ---
  ```
</ParamField>

<ParamField path="Astro.getActionResult" type="<TAction>(action: TAction) => ActionReturnType<TAction> | undefined">
  Get the result of an Action submission when using a form POST.

  ```astro theme={null}
  ---
  import { actions } from 'astro:actions';

  const result = Astro.getActionResult(actions.myAction);
  ---
  {result?.data && <p>Success: {result.data}</p>}
  {result?.error && <p>Error: {result.error.message}</p>}
  ```
</ParamField>

## Session

<ParamField path="Astro.session" type="AstroSession | undefined">
  Utilities for handling sessions in on-demand rendered routes.

  ```astro theme={null}
  ---
  // Get session data
  const userId = await Astro.session.get('userId');

  // Set session data
  await Astro.session.set('userId', '123');

  // Destroy session
  await Astro.session.destroy();
  ---
  ```
</ParamField>

## CSP (Content Security Policy)

<ParamField path="Astro.csp" type="object | undefined">
  Utilities to control CSP headers. Only available when CSP is enabled in config.

  ### Methods

  <ParamField path="Astro.csp.insertDirective" type="(directive: string) => void">
    Add a specific CSP directive to the route being rendered.

    ```astro theme={null}
    ---
    Astro.csp?.insertDirective("default-src 'self' https://example.com");
    ---
    ```
  </ParamField>

  <ParamField path="Astro.csp.insertStyleResource" type="(payload: string) => void">
    Set the resource for the `style-src` directive.

    ```astro theme={null}
    ---
    Astro.csp?.insertStyleResource('https://styles.cdn.example.com/');
    ---
    ```
  </ParamField>

  <ParamField path="Astro.csp.insertStyleHash" type="(hash: string) => void">
    Insert a single style hash to the route being rendered.

    ```astro theme={null}
    ---
    Astro.csp?.insertStyleHash('sha256-1234567890abcdef');
    ---
    ```
  </ParamField>

  <ParamField path="Astro.csp.insertScriptResource" type="(resource: string) => void">
    Set the resource for the `script-src` directive.

    ```astro theme={null}
    ---
    Astro.csp?.insertScriptResource('https://scripts.cdn.example.com/');
    ---
    ```
  </ParamField>

  <ParamField path="Astro.csp.insertScriptHash" type="(hash: string) => void">
    Insert a single script hash to the route being rendered.

    ```astro theme={null}
    ---
    Astro.csp?.insertScriptHash('sha256-1234567890abcdef');
    ---
    ```
  </ParamField>
</ParamField>
