Skip to main content
Astro provides a programmatic JavaScript API that allows you to run Astro commands from code. This is useful for building tools, integrations, or custom workflows.
The JavaScript API is experimental and may change in future releases.

Available Functions

The Astro JavaScript API exports the following functions:

build()

Build your Astro project for production

dev()

Start the Astro development server

preview()

Preview your production build locally

sync()

Generate TypeScript types for your project

Installation

Install Astro in your project:

Basic Usage

Import the functions you need from the astro package:

Configuration

All API functions accept an AstroInlineConfig object that allows you to configure Astro programmatically. This object supports all the same options as astro.config.mjs.

Common Options

These options are available for all API functions:
string
default:"process.cwd()"
The root directory of your Astro project. Can be an absolute path or relative to the current working directory.
'debug' | 'info' | 'warn' | 'error' | 'silent'
default:"'info'"
The logging level for Astro’s output.
string
default:"'production'"
The mode to run Astro in. This affects environment variable loading and other build-time behavior.
string
Your deployed site URL. Used for generating sitemaps and canonical URLs.
string
default:"'/'"
The base path where your site is deployed.
string
default:"'./dist'"
The directory where build output is written.
boolean
default:"false"
Force a clean build by clearing the content layer cache.

Use Cases

Build Tools

Use the JavaScript API to integrate Astro into your existing build pipeline:

Testing

Start a dev server for integration tests:

Custom Integrations

Build custom tooling that leverages Astro’s type generation:

Next Steps

Explore the individual API function references for detailed parameter information and examples: