Set up a Mastra project with npm create mastra, add your API keys, and open Mastra Studio on localhost:4111. A beginner friendly Mastra setup guide.
Set up a working TypeScript environment for your customer support assistant using the Mastra CLI, configure your API keys, and run Mastra Studio locally.
Previous lesson: Mastra Overview: What Is Mastra? | Next lesson: Building Your First Mastra Agent
Before you run any commands, make sure you have Node.js version 22.18.0 or later installed on your computer. You can check your version by running node -v in your terminal.
The fastest way to scaffold a new project is with the Mastra CLI. Open your terminal, navigate to the directory where you store your projects, and run this command:
bashCopy
The interactive prompt will ask you for a project name. Name it customer-support-agent. The CLI will create a new directory, initialize a package.json file, set up TypeScript, and install the required dependencies.
If you prefer to initialize the directory by hand instead of using the generator, you can install the packages manually:
bashCopy
Mastra requires ECMAScript modules. The package.json file must contain "type": "module" so Node.js loads your TypeScript files correctly through the Mastra development server.
Common Mistake: Forgetting to set "type": "module" in package.json causes module resolution errors when importing @mastra/core. Verify that this field is present before proceeding.
If you set the project up by hand, you also need a tsconfig.json file. The npm create mastra@latest command generates one for you, so the generator is the easier path for beginners.
Later lessons run small TypeScript test scripts with tsx. Install it as a development dependency now, whichever setup path you chose:
bashCopy
A standard Mastra project isolates framework code inside a dedicated folder. This structure keeps your application organized as you add agents, tools, and workflows throughout this course.
Think of the src/mastra directory like an air traffic control tower. The control tower houses the central registry, while the runways and gates hold your specific agents, tools, and workflows.
Your project should follow this file tree:
textCopy
Here is what each item does:
src/mastra/index.ts: The main entry point. You export an instance of the Mastra class from this file.src/mastra/agents/: Directory where you define your agent instances and instructions.src/mastra/tools/: Directory where you define custom tools using schemas.src/mastra/workflows/: Directory where you build deterministic, step-by-step processes..env: A plain text file that contains your secrets and API keys.Quick Insight: Mastra looks for src/mastra/index.ts by default when starting the local dev server. Keeping this entry point clean ensures the CLI can automatically discover all registered resources.
Mastra delegates model execution to large language model providers. For this course, you will use OpenAI models like openai/gpt-5-mini.
Create a file named .env in the root of your project:
bashCopy
Add your provider keys inside .env:
envCopy
If you plan to test other providers later, you can also include their keys in the same file:
envCopy
npm run dev loads the variables in .env automatically, so the dev server and Mastra Studio need no extra setup. You do not need to install dotenv.
Standalone test scripts are different. When you run a script with tsx in later lessons, .env is not loaded on its own, so those commands include the --env-file=.env flag.
Pro Tip: Never commit .env to version control. Add .env to your .gitignore file immediately so your secret keys remain private.
To start the development server without errors, you need a valid entry point file that exports a Mastra instance.
Create the file src/mastra/index.ts and paste the following complete code block into it:
tsCopy
This snippet initializes a Mastra instance with empty registries for agents and workflows. In the upcoming lessons, you will import and register your customer support agent, custom order lookup tools, and ticket workflows directly into this object.
Try This: Open your package.json file and verify that you have a dev script defined. It should look like this:
jsonCopy
If the script is missing, add it to your scripts block so you can start Mastra using standard npm commands.
Now you are ready to boot up the environment. Run the development command in your terminal:
bashCopy
The CLI starts the local server and outputs two important URLs:
http://localhost:4111http://localhost:4111/swagger-uiOpen http://localhost:4111 in your web browser. Mastra Studio is a visual dashboard that connects directly to your local codebase. From Studio, you can:
The Swagger interface at http://localhost:4111/swagger-ui exposes auto-generated REST endpoints for every agent and workflow registered on your Mastra instance. This means you can integrate your Mastra backend with web hooks, frontend apps, or external services without writing custom HTTP handlers.
Quick Challenge: While npm run dev is running, visit http://localhost:4111/swagger-ui and check the OpenAPI schema generated by your empty Mastra instance.
node -v to confirm you have Node.js 22.18.0 or later.package.json contains "type": "module".OPENAI_API_KEY to your .env file.src/mastra/index.ts file provided in this lesson.npm run dev and navigate to http://localhost:4111 to confirm that Mastra Studio opens cleanly.In the next lesson, Building Your First Mastra Agent, you will define your first support agent with custom system instructions and test it inside Mastra Studio.
Was this lesson helpful?