Deployment
In this guide, we’ll cover how to deploy your Mastra agents and workflows using the mastra deploy
command. Once deployed, your agents and workflows will be available via REST endpoints.
Currently, the Mastra CLI supports deploying to Vercel and Cloudflare Workers.
Prerequisites
Before you begin, ensure you have the following:
- Node.js installed (version 18 or higher is recommended).
- Mastra CLI installed globally:
npm install -g mastra
- An existing Mastra project set up. If not, initialize one using:
mastra init
- A Vercel account (if deploying to Vercel) or a Cloudflare account (if deploying to Cloudflare Workers).
Deploying to Vercel
Vercel is a cloud platform for static sites and serverless functions. Mastra can be easily deployed to Vercel using the CLI.
Steps to Deploy
- Install the Vercel CLI (if you haven’t already):
npm install -g vercel
- Login to Vercel:
vercel login
- Deploy with Mastra CLI:
Navigate to your project directory and run:
mastra deploy vercel
The CLI will guide you through the deployment process, including:
- Authentication: If you haven’t provided a Vercel token before, you’ll be prompted to enter one.
- Team Selection: Choose the Vercel team or scope to deploy under.
- Environment Variables: Set any required environment variables (e.g.,
OPENAI_API_KEY
,ANTHROPIC_API_KEY
).
- Set Environment Variables on Vercel (Optional):
If you didn’t set environment variables during deployment, you can configure them in the Vercel dashboard:
- Go to your Vercel project.
- Navigate to Settings > Environment Variables.
- Add your variables (e.g.,
OPENAI_API_KEY
,ANTHROPIC_API_KEY
).
Example
cd your-mastra-project
mastra deploy vercel
Follow the prompts provided by the CLI to complete the deployment.
Deploying to Cloudflare Workers
Cloudflare Workers allow you to deploy serverless applications to Cloudflare’s edge network.
Steps to Deploy
- Install the Wrangler CLI:
npm install -g wrangler
- Login to Cloudflare:
wrangler login
- Deploy with Mastra CLI:
Navigate to your project directory and run:
mastra deploy cloudflare
The CLI will guide you through the process, including:
- Authentication: If you haven’t logged in, you’ll be prompted to do so.
- Project Configuration: The CLI will generate a
wrangler.toml
file with necessary settings. - Environment Variables: Set any required variables in the
wrangler.toml
file or via the Cloudflare dashboard.
- Set Environment Variables in
wrangler.toml
:
Edit the wrangler.toml
file in your project directory to include your environment variables:
[vars]
OPENAI_API_KEY = "your-openai-api-key"
ANTHROPIC_API_KEY = "your-anthropic-api-key"
Example
cd your-mastra-project
mastra deploy cloudflare
Follow the prompts provided by the CLI to complete the deployment.
Additional Tips
Ensure Your mastra
Instance is Exported Properly
Make sure that your mastra
instance is exported in your entry file (e.g., src/mastra/index.ts
):
import { Mastra } from '@mastra/core';
export const mastra = new Mastra({
// Your configuration here
});
Conclusion
Congratulations! You’ve successfully deployed your Mastra agents and workflows. They’re live on the internet and ready to be used by your users.