Deploy Mastra to Netlify
Use @mastra/deployer-netlify to deploy your mastra server as serverless functions on Netlify. The deployer bundles your code and generates a .netlify directory conforming to Netlify's frameworks API, ready to deploy.
If you're using a server adapter or web framework, deploy the way you normally would for that framework.
Before you beginDirect link to Before you begin
You'll need a Mastra application and a Netlify account.
Netlify Functions use an ephemeral filesystem, so any storage you configure (including observability storage) must be hosted externally. If you're using LibSQLStore with a file URL, switch to a remotely hosted database.
InstallationDirect link to Installation
Add the @mastra/deployer-netlify package to your project:
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/deployer-netlify@latest
pnpm add @mastra/deployer-netlify@latest
yarn add @mastra/deployer-netlify@latest
bun add @mastra/deployer-netlify@latest
Import NetlifyDeployer and set it as the deployer in your Mastra configuration:
import { Mastra } from "@mastra/core";
import { NetlifyDeployer } from "@mastra/deployer-netlify";
export const mastra = new Mastra({
deployer: new NetlifyDeployer(),
});
Create a netlify.toml file with the following contents in your project root:
[build]
command = "mastra build"
UsageDirect link to Usage
After setting up your project, push it to your remote Git provider of choice (e.g. GitHub).
Connect your repository to Netlify. During the setup process, Netlify will detect your
netlify.tomlfile and set the build command tomastra build.noteRemember to set your environment variables needed to run your application (e.g. your model provider API key).
Once you're ready, click the Deploy button and wait for the first deployment to complete. It'll tell you that one function has been deployed.
Try out your newly deployed function by going to
https://<random-slug>.netlify.app/api/agents. You should get a JSON response listing all available agents.Since the Mastra server prefixes every API endpoint with
/api, you have to add it to your URLs when making requests.infoNetlify functions are typically deployed at
https://<random-slug>.netlify.app/.netlify/functions/api. TheNetlifyDeployerredirects any request from/*to/.netlify/functions/api/:splat, so you don't need to include the.netlify/functionsprefix in your URLs.You can now call your Mastra endpoints over HTTP.
noteSet up authentication before exposing your endpoints publicly.