Skip to main content
Astro Integrations add new functionality and behaviors for your project with just a few lines of code. This page describes the API for building your own integrations.

Integration Structure

An Astro integration is an object with a name and hooks property:

Basic Example

Integration Hooks

Integrations can define hook functions that run at specific times during Astro’s lifecycle. All hooks are optional.

astro:config:setup

function
Called during Astro initialization, before the Vite or Astro config have been resolved. This hook can be used to extend project configuration.Parameters:
  • config - A read-only copy of the user-supplied Astro config
  • command - The command used to run Astro: 'dev', 'build', 'preview', or 'sync'
  • isRestart - Whether the dev server is restarting
  • updateConfig - Function to update the user config
  • addRenderer - Add a framework renderer (React, Vue, etc.)
  • addWatchFile - Add a file to watch for changes
  • injectScript - Inject a client script into pages
  • injectRoute - Add a new route to the Astro project
  • addClientDirective - Add a custom client directive
  • addDevToolbarApp - Add a custom dev toolbar app
  • addMiddleware - Add middleware
  • createCodegenDir - Create a directory for generated code
  • logger - Integration logger instance

astro:config:done

function
Called after the Astro config has been fully resolved and other integrations have run their astro:config:setup hooks.Parameters:
  • config - The resolved Astro config
  • setAdapter - Set the adapter for SSR
  • injectTypes - Inject TypeScript type definitions
  • logger - Integration logger instance
  • buildOutput - The build output mode: 'static' or 'server'

astro:server:setup

function
Called during the dev server setup, before the server starts. Allows access to the Vite dev server instance.Parameters:
  • server - The Vite dev server instance
  • logger - Integration logger instance
  • toolbar - Dev toolbar communication helpers
  • refreshContent - Function to refresh content collections (optional)

astro:server:start

function
Called after the dev server has started.Parameters:
  • address - The server address information (host, port, etc.)
  • logger - Integration logger instance

astro:server:done

function
Called when the dev server is closed.Parameters:
  • logger - Integration logger instance

astro:build:start

function
Called when the build process starts.Parameters:
  • logger - Integration logger instance
  • setPrerenderer - Set a custom prerenderer (advanced)

astro:build:setup

function
Called during the build, allows you to extend the Vite configuration used during the build.Parameters:
  • vite - Vite config for the build
  • pages - Map of page paths to their build data
  • target - Build target: 'client' or 'server'
  • updateConfig - Function to update the Vite config
  • logger - Integration logger instance

astro:build:generated

function
Called after a static build has generated all routes and assets.Parameters:
  • dir - The build output directory
  • logger - Integration logger instance
  • routeToHeaders - Map of routes to their HTTP headers (when adapter supports static headers)

astro:build:ssr

function
Called after an SSR build has completed.Parameters:
  • manifest - The serialized SSR manifest
  • middlewareEntryPoint - File path of the emitted middleware
  • logger - Integration logger instance

astro:build:done

function
Called after the build has completed and all output files have been written to disk.Parameters:
  • pages - Array of all generated pages with pathname info
  • dir - The build output directory
  • assets - Map of asset file names to their output URLs
  • logger - Integration logger instance

astro:route:setup

function
Called for each route during routing setup. Allows you to modify route options.Parameters:
  • route - Route options including component path and prerender setting
  • logger - Integration logger instance

astro:routes:resolved

function
Called after all routes have been resolved.Parameters:
  • routes - Array of all resolved routes in the project
  • logger - Integration logger instance

Adding Framework Renderers

Use the addRenderer function in the astro:config:setup hook to add support for UI frameworks:

Injecting Scripts

Use the injectScript function to add JavaScript to pages:
Script stages:
  • 'before-hydration' - Imported client-side, before hydration
  • 'head-inline' - Injected into a script tag in the <head>
  • 'page' - Injected into the JavaScript bundle of every page
  • 'page-ssr' - Injected into the frontmatter of every Astro page

Adding Routes

Use the injectRoute function to add new routes:

Adding Middleware

Use the addMiddleware function to add middleware:

Client Directives

Add custom client directives for controlling component hydration:

Dev Toolbar Apps

Add custom apps to the Astro dev toolbar:

TypeScript Types

Inject TypeScript type definitions into the user’s project:

Integration Logger

All hooks receive a logger instance for consistent logging:

Building Adapters

Adapters are a special type of integration that set the adapter configuration. Use the setAdapter function in astro:config:done:

Adapter Features

supportedAstroFeatures

Defines which Astro features the adapter supports:
  • staticOutput - Serving static pages
  • serverOutput - On-demand rendered pages
  • hybridOutput - Mix of static and on-demand pages
  • i18nDomains - i18n domain routing
  • envGetSecret - Retrieving secrets from astro:env/server
  • sharpImageService - Image transformation with Sharp
Each feature can be:
  • 'stable' - Fully supported
  • 'experimental' - Experimental support
  • 'deprecated' - Deprecated support
  • 'unsupported' - Not supported
  • { support: 'limited', message: 'Custom message' } - Limited support with explanation

adapterFeatures

Controls adapter-specific build features:
  • buildOutput - Force 'static' or 'server' output
  • middlewareMode - 'classic' or 'edge' middleware execution
  • staticHeaders - Support for setting headers on static pages

Complete Integration Example