# Wasp 0.25 Full Documentation
This is the full documentation for the latest version of Wasp.
For other versions, see the links below.
## Full Documentation by Version
- [latest (currently 0.25)](https://wasp.sh/llms-full.txt)
- [0.25](https://wasp.sh/llms-full-0.25.txt)
- [0.24](https://wasp.sh/llms-full-0.24.txt)
- [0.23](https://wasp.sh/llms-full-0.23.txt)
- [0.22](https://wasp.sh/llms-full-0.22.txt)
- [0.21](https://wasp.sh/llms-full-0.21.txt)
- [0.20](https://wasp.sh/llms-full-0.20.txt)
- [0.19](https://wasp.sh/llms-full-0.19.txt)
- [0.18](https://wasp.sh/llms-full-0.18.txt)
- [0.17](https://wasp.sh/llms-full-0.17.txt)
- [0.16](https://wasp.sh/llms-full-0.16.txt)
- [0.15](https://wasp.sh/llms-full-0.15.txt)
- [0.14](https://wasp.sh/llms-full-0.14.txt)
- [0.13](https://wasp.sh/llms-full-0.13.txt)
- [0.12](https://wasp.sh/llms-full-0.12.txt)
- [0.11.8](https://wasp.sh/llms-full-0.11.8.txt)
---
# Docs
## Getting Started / Introduction
:::note
If you are looking for the installation instructions, check out the [Quick Start](https://wasp.sh/docs/quick-start) section.
:::
We will give a brief overview of what Wasp is, how it works on a high level and when to use it.
### Wasp is a tool to build modern web applications
It is an opinionated way of building **full-stack web applications**. It takes care of all three major parts of a web application: **client** (front-end), **server** (back-end) and **database**.
#### Works well with your existing stack
Wasp is not trying to do everything at once but rather focuses on the complexity that arises from connecting all the parts of the stack (client, server, database, deployment).
Wasp is using **React**, **Node.js** and **Prisma** under the hood and relies on them to define web components and server queries and actions.
#### Wasp's secret sauce
At the core is the Wasp compiler which takes the Wasp Spec and your Javascript code and outputs the client app, server app and deployment code.

How the magic happens ๐
The cool thing about having a compiler that understands your code is that it can do a lot of things for you.
Define your app in the Wasp file and get:
- login and signup with Auth UI components,
- full-stack type safety,
- e-mail sending,
- async processing jobs,
- React Query powered data fetching,
- security best practices,
- and more.
You don't need to write any code for these features, Wasp will take care of it for you ๐คฏ And what's even better, Wasp also maintains the code for you, so you don't have to worry about keeping up with the latest security best practices. As Wasp updates, so does your app.
### So what does the code look like?
Let's say you want to build a web app that allows users to **create and share their favorite recipes**.
Let's start with the `main.wasp.ts` file: it is the central spec file of your app, where you describe the app from the high level.
Let's give our app a title and let's immediately turn on the full-stack authentication via username and password:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "RecipeApp",
wasp: { version: "^0.25" },
title: "My Recipes",
head: [""],
auth: {
methods: { usernameAndPassword: {} },
onAuthFailedRedirectTo: "/login",
userEntity: "User",
},
// ...
})
```
Let's then add the data models for your recipes. Wasp understands and uses the models from the `schema.prisma` file. We will want to have Users and Users can own Recipes:
```prisma title="schema.prisma"
...
// Data models are defined using Prisma Schema Language.
model User {
id Int @id @default(autoincrement())
recipes Recipe[]
}
model Recipe {
id Int @id @default(autoincrement())
title String
description String?
userId Int
user User @relation(fields: [userId], references: [id])
}
```
Next, let's define how to do something with these data models!
We do that by defining Operations, in this case, a Query `getRecipes` and Action `addRecipe`, which are in their essence Node.js functions that execute on the server and can, thanks to Wasp, very easily be called from the client.
First, we define these Operations in our `main.wasp.ts` file, so Wasp knows about them and can "beef them up":
```ts title="main.wasp.ts"
import { action, app, query } from "@wasp.sh/spec"
import { getRecipes, addRecipe } from "./src/recipe/operations" with { type: "ref" }
export default app({
// ...
spec: [
// ...
// Queries have automatic cache invalidation and are type-safe.
query(getRecipes, { entities: ["Recipe"] }),
// Actions are type-safe and can be used to perform side-effects.
action(addRecipe, { entities: ["Recipe"] }),
],
})
```
... and then implement them in our Javascript (or TypeScript) code (we show just the query here, using TypeScript):
```ts title="src/recipe/operations.ts"
// Wasp generates the types for you.
import { type GetRecipes } from "wasp/server/operations";
import { type Recipe } from "wasp/entities";
export const getRecipes: GetRecipes<{}, Recipe[]> = async (_args, context) => {
return context.entities.Recipe.findMany( // Prisma query
{ where: { user: { id: context.user.id } } }
);
};
export const addRecipe ...
```
Now we can very easily use these in our React components!
For the end, let's create a home page of our app.
First, we define it in `main.wasp.ts`:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { HomePage } from "./src/pages/HomePage" with { type: "ref" }
export default app({
// ...
spec: [
// ...
route("HomeRoute", "/", page(HomePage, {
authRequired: true, // Will send user to /login if not authenticated.
})
),
],
})
```
and then implement it as a React component in JS/TS (that calls the Operations we previously defined):
```tsx title="src/pages/HomePage.tsx"
import { useQuery, getRecipes } from "wasp/client/operations"
import { type User } from "wasp/entities"
export function HomePage({ user }: { user: User }) {
// Due to full-stack type safety, `recipes` will be of type `Recipe[]` here.
const { data: recipes, isLoading } = useQuery(getRecipes) // Calling our query here!
if (isLoading) {
return
Loading...
}
return (
Recipes
{recipes
? recipes.map((recipe) => (
{recipe.title}
{recipe.description}
))
: "No recipes defined yet!"}
)
}
```
And voila! We are listing all the recipes in our app ๐
This was just a quick example to give you a taste of what Wasp is. For step by step tour through the most important Wasp features, check out the [Todo App tutorial](https://wasp.sh/docs/tutorial/create).
:::note
Above we skipped defining `/login` and `/signup` pages to keep the example a bit shorter, but those are very simple to do by using Wasp's Auth UI feature.
:::
### When to use Wasp
Wasp addresses the same core problems that typical web app frameworks are addressing, and it in big part [looks, swims and quacks](https://en.wikipedia.org/wiki/Duck_test) like a web app framework.
#### Best used for
- building full-stack web apps (like e.g. Airbnb or Asana)
- quickly starting a web app with industry best practices
- to be used alongside modern web dev stack (React and Node.js are currently supported)
#### Avoid using Wasp for
- building static/presentational websites
- to be used as a no-code solution
- to be a solve-it-all tool in a single language
### Wasp is a spec-driven framework
Wasp does not match typical expectations of a web app framework: it is not just a set of libraries. You describe your app in the `main.wasp.ts` spec file, and the compiler uses that spec together with your React, Node.js, and Prisma code to generate the application structure and glue code.
This spec-driven approach lets Wasp focus on one purpose: **building modern web applications with 10x less code and less stack-specific knowledge**.
## Getting Started / Quick Start
### Installation
Welcome, new Waspeteer ๐!
Let's create and run our first Wasp app in 3 short steps:
1. **To install Wasp on Linux / OSX / WSL (Windows), open your terminal and run:**
```shell
npm i -g @wasp.sh/wasp-cli@latest
```
โน๏ธ Wasp requires Node.js and npm, which are usually installed together: check below for [more details](#requirements).
2. **Then, create a new app by running:**
```shell
wasp new
```
3. **Finally, run the app:**
```shell
cd
wasp start
```
That's it ๐ You have successfully created a new full-stack web app at and Wasp is serving both frontend and backend for you.
But don't stop there! Turn your coding agent into a Wasp framework expert in the next step.
4. **Install Agent Plugin / Skills:**
**Claude Code:**
```bash
claude plugin marketplace add wasp-lang/wasp-agent-plugins
claude plugin install wasp@wasp-agent-plugins --scope project
```
**Other Agents (Cursor, Codex, Gemini, Copilot, OpenCode, etc.):**
```bash
npx skills add wasp-lang/wasp-agent-plugins
```
**Initialize the plugin / skills:** Invoke the `/wasp-plugin-init` skill to add Wasp knowledge to your agent's memory file (e.g. `CLAUDE.md`, `AGENTS.md`):
```bash
Run the '/wasp-plugin-init' skill.
```
For more info check out the [Wasp Agent Plugin / Skills](https://wasp.sh/docs/wasp-ai/coding-agent-plugin) page.
:::note[Something Unclear?]
Check [More Details](#more-details) section below if anything went wrong with the installation, or if you have additional questions.
:::
:::tip[Having trouble running Wasp?]
If you get stuck with a weird error while developing with Wasp, try running `wasp clean` - this is the Wasp equivalent of "turning it off and on again"! Do however let us know about the issue on our GitHub repo or Discord server.
:::
#### What next?
- [ ] ๐ **Check out the [Todo App tutorial](https://wasp.sh/docs/tutorial/create), which will take you through all the core features of Wasp!** ๐
- [ ] [Setup your editor](https://wasp.sh/docs/editor-setup) for working with Wasp.
- [ ] Join us on [Discord](https://discord.gg/rzdnErX)! Any feedback or questions you have, we are there for you.
- [ ] Follow Wasp development by subscribing to our newsletter: . We usually send 1 per month, and [Matija](https://github.com/matijaSos) does his best to unleash his creativity to make them engaging and fun to read \:D!
---
### More details
#### Requirements
You must have Node.js (and NPM) installed on your machine and available in `PATH`.
A version of Node.js must be >= 24.14.1.
If you need it, we recommend using [nvm](https://github.com/nvm-sh/nvm) for managing your Node.js installation version(s).
A quick guide on installing/using nvm
Install nvm via your OS package manager (`apt`, `pacman`, `homebrew`, ...) or via the [nvm](https://github.com/nvm-sh/nvm#install--update-script) install script.
Then, install a version of Node.js that you need:
```shell
nvm install 24
```
Finally, whenever you need to ensure a specific version of Node.js is used, run:
```shell
nvm use 24
```
to set the Node.js version for the current shell session.
You can run
```shell
node -v
```
to check the version of Node.js currently being used in this shell session.
Check NVM repo for more details: .
#### Installation {#detailed-installation}
**Linux / macOS (npm)**
Open your terminal and run:
```shell
npm i -g @wasp.sh/wasp-cli@latest
```
:::note[Looking for the old installer script?]
Check out the **[Legacy installer guide](https://wasp.sh/docs/guides/legacy/installer)** for instructions on how to keep using it or how to migrate to npm-based installation.
:::
**Windows**
With Wasp for Windows, we are almost there: Wasp is successfully compiling and running on Windows but there is a bug or two stopping it from fully working. Check it out [here](https://github.com/wasp-lang/wasp/issues/48) if you are interested in helping.
In the meantime, the best way to start using Wasp on Windows is by using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install). Once you set up Ubuntu on WSL, just follow Linux instructions for installing Wasp. You can refer to this [article](https://wasp.sh/blog/2023/11/21/guide-windows-development-wasp-wsl) if you prefer a step by step guide to using Wasp in WSL environment. If you need further help, reach out to us on [Discord](https://discord.gg/rzdnErX) - we have some community members using WSL that might be able to help you.
:::caution
If you are using WSL2, make sure that your Wasp project is not on the Windows file system, but instead on the Linux file system. Otherwise, Wasp won't be able to detect file changes, due to the [issue in WSL2](https://github.com/microsoft/WSL/issues/4739). If you use VS Code, install the [WSL extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl) and open the project from inside WSL (run `code .` in the project directory). Without it, TypeScript support in VS Code won't work on WSL2.
:::
**From source**
If the other methods are not working for you or your OS is not supported, you can try building Wasp from the source.
To install from source, you need to clone the [wasp repo](https://github.com/wasp-lang/wasp), install [Cabal](https://cabal.readthedocs.io/en/stable/getting-started.html) on your machine and then run `cabal install` from the `waspc/` dir.
If you have never built Wasp before, this might take some time due to `cabal` downloading dependencies for the first time.
Check [waspc/](https://github.com/wasp-lang/wasp/tree/main/waspc) for more details on building Wasp from the source.
## Getting Started / Editor Setup
:::note
This page assumes you have already installed Wasp. If you do not have Wasp installed yet, check out the [Quick Start](https://wasp.sh/docs/quick-start) guide.
:::
Wasp Spec files are TypeScript files, so editor support comes from your editor's regular TypeScript tooling.
### TypeScript support
Use any editor with TypeScript language service support (VS Code, Zed, etc.), this gives you:
- type checking and diagnostics for `main.wasp.ts`
- autocompletion for `@wasp.sh/spec` functions and options
- go to definition for [reference imports](https://wasp.sh/docs/general/spec#reference-imports)
- import path checks for files in `src`
For `schema.prisma`, install the [Prisma extension for VS Code](https://marketplace.visualstudio.com/items?itemName=Prisma.prisma) or the equivalent Prisma extension for your editor.
If your editor reports stale type or import errors after changing Wasp files, restart the TypeScript server. In VS Code, open the command palette and run *"TypeScript: Restart TS Server."*
## Tutorial / 1. Creating a New Project
:::info
You'll need to have the latest version of Wasp installed locally to follow this tutorial. If you haven't installed it yet, check out the [QuickStart](https://wasp.sh/docs/quick-start) guide!
:::
In this section, we'll guide you through the process of creating a simple Todo app with Wasp. In the process, we'll take you through the most important and useful features of Wasp.

If you get stuck at any point (or just want to chat), reach out to us on [Discord](https://discord.gg/rzdnErX) and we will help you!
You can find the complete code of the app we're about to build [here](https://github.com/wasp-lang/wasp/tree/release/examples/tutorials/TodoApp).
### Creating a Project
To setup a new Wasp project, run the following command in your terminal:
```sh
wasp new TodoApp -t minimal
```
We are using the `minimal` template because we're going to implement the app from scratch, instead of the more full-featured default template.
Enter the newly created directory and start the development server:
```sh
cd TodoApp
wasp start
```
`wasp start` will take a bit of time to start the server the first time you run it in a new project.
You will see log messages from the client, server, and database setting themselves up. When everything is ready, a new tab should open in your browser at `http://localhost:3000` with a simple placeholder page:

Wasp has generated for you the full front-end and back-end code of the app! Next, we'll take a closer look at how the project is structured.
### A note on supported languages
Wasp supports both JavaScript and TypeScript out of the box, but you are free to choose between or mix JavaScript and TypeScript as you see fit.
We'll provide you with both JavaScript and TypeScript code in this tutorial. Code blocks will have a toggle to switch between vanilla JavaScript and TypeScript.
Try it out:
:::note[Welcome to TypeScript!]
You are now reading the TypeScript version of the docs. The site will remember your preference as you switch pages.
You'll have a chance to change the language on every code snippet - both the snippets and the text will update accordingly.
:::
## Tutorial / 2. Project Structure
After creating a new Wasp project, your project should look like this:
```python
.
โโโ AGENTS.md # Instructions for AI coding agents.
โโโ CLAUDE.md # Symlink to AGENTS.md.
โโโ main.wasp.ts # Your Wasp Spec goes here.
โโโ package.json # Your dependencies and project info go here.
โโโ public # Your static files (e.g., images, favicon) go here.
โย ย โโโ favicon.ico
โโโ schema.prisma # Your database models go here.
โโโ src # Your source code (TS/React/Node.js) goes here.
โย ย โโโ Main.css
โย ย โโโ MainPage.tsx
โย ย โโโ assets
โย ย โย ย โโโ wasp-logo-rounded.svg
โย ย โโโ vite-env.d.ts
โโโ tsconfig.json
โโโ tsconfig.src.json
โโโ tsconfig.wasp.json
โโโ vite.config.ts
```
By *your code*, we mean the *"the code you write"*, as opposed to the code generated by Wasp. Wasp allows you to organize and structure your code however you think is best - there's no need to separate client files and server files into different directories.
We'd normally recommend organizing code by features (i.e., vertically). However, since this tutorial contains only a handful of files, there's no need for fancy organization. We'll keep it simple by placing everything in the root `src` directory.
Many other files (e.g., `tsconfig.json`, `tsconfig.src.json`, `tsconfig.wasp.json`, `vite-env.d.ts`, etc.) help Wasp and the IDE improve your development experience with autocompletion, IntelliSense, and error reporting.
The `vite.config.ts` file is used to configure [Vite](https://vitejs.dev/guide/), Wasp's build tool of choice. We won't be customizing the Vite setup in this tutorial, so you can safely ignore the file. Still, if you ever end up wanting more control over Vite, you'll find everything you need to know in [custom Vite config docs](https://wasp.sh/docs/project/custom-vite-config).
The `schema.prisma` file is where you define your database schema using [Prisma](https://www.prisma.io/). We'll cover this a bit later in the tutorial.
The most important file in the project is `main.wasp.ts`. Wasp uses the configuration within it to perform its magic. Based on what you write, it generates a bunch of code for your database, server-client communication, React routing, and more.
Let's take a closer look at `main.wasp.ts`
### `main.wasp.ts`
`main.wasp.ts` is your app's Wasp file. It defines the app's central components and helps Wasp to do a lot of the legwork for you.
The file exports your app's top-level configuration and a collection of specifications. Each one defines a Route, Page, Query, Action, or other features provided by Wasp.
The default `main.wasp.ts` file generated with `wasp new` on the previous page looks like this:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
// This is a reference to your MainPage component.
// Read more about how Wasp references your code in the section below.
import { MainPage } from "./src/MainPage" with { type: "ref" }
export default app({
name: "TodoApp",
// Pins the version of Wasp to use.
wasp: { version: "^0.25" },
title: "TodoApp", // Used as the browser tab title.
head: [""],
// Add your specs here so Wasp knows to register them.
spec: [
route("RootRoute", "/", page(MainPage)),
],
})
```
#### Referencing code from `src`
When `main.wasp.ts` needs to point to your React components or Node.js functions, it uses imports like this:
```ts
import { MainPage } from "./src/MainPage" with { type: "ref" }
```
Notice the `with { type: "ref" }` part at the end of the import statement. This tells Wasp to treat the import as a reference to your app's code, without running the imported code. For more details and examples, see [reference imports](https://wasp.sh/docs/general/spec#reference-imports).
#### Specifications
This Wasp app uses three specifications:
- [**app**](https://wasp.sh/docs/api/@wasp.sh/spec/functions/app): Top-level configuration information about your app.
- [**route**](https://wasp.sh/docs/api/@wasp.sh/spec/functions/route): Describes which path each page should be accessible from.
- [**page**](https://wasp.sh/docs/api/@wasp.sh/spec/functions/page): Defines a web page and the React component that gets rendered when the page is loaded.
In the next section, we'll explore how **route** and **page** work together to build your web app.
## Tutorial / 3. Pages & Routes
In the default `main.wasp.ts` file created by `wasp new`, there is a **page** and a **route** spec:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
export default app({
// ...
spec: [
// We specify that the React implementation of the page is exported from
// `src/MainPage.tsx`. Reference imports must point to files inside `src`.
route("RootRoute", "/", page(MainPage)),
],
})
```
Together, these specifications tell Wasp that when a user navigates to `/`, it should render the `MainPage` component from `src/MainPage.tsx`.
### The MainPage Component
Let's take a look at the React component referenced by the page spec:
```tsx title="src/MainPage.tsx"
import Logo from "./assets/wasp-logo-rounded.svg";
import "./Main.css";
export function MainPage() {
// ...
}
```
This is a regular functional React component. It also imports some CSS and a logo from the `assets` folder.
That is all the code you need! Wasp takes care of everything else necessary to define, build, and run the web app.
:::tip[Keep Wasp start running]
`wasp start` automatically picks up the changes you make, regenerates the code, and restarts the app. So keep it running in the background.
It also improves your experience by tracking the working directory and ensuring the generated code/types are up to date with your changes.
:::
:::caution[LSP Problems]
If you are using TypeScript, your editor may sometimes report type and import errors even while `wasp start` is running.
This happens when the TypeScript Language Server gets out of sync with the current code. If you're using VS Code, you can manually restart the language server by opening the command palette and selecting *"TypeScript: Restart TS Server."* Open the command pallete with:
- `Ctrl` + `Shift` + `P` if you're on Windows or Linux.
- `Cmd` + `Shift` + `P` if you're on a Mac.
:::
### Adding a Second Page
To add more pages, you can add another route to your spec. You can even add parameters to the URL path, using [dynamic segments](https://wasp.sh/docs/advanced/routing#dynamic-segments). Let's test this out by adding a new page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { HelloPage } from "./src/HelloPage" with { type: "ref" }
export default app({
// ...
spec: [
route("HelloRoute", "/hello/:name", page(HelloPage)),
],
})
```
When a user visits `/hello/their-name`, Wasp renders the component exported from `src/HelloPage.tsx` and you can use the `useParams` hook from `react-router` to access the `name` parameter:
```tsx title="src/HelloPage.tsx"
import { useParams } from "react-router";
export const HelloPage = () => {
const { name } = useParams<"name">();
return
Here's {name}!
;
};
```
Now you can visit `/hello/johnny` and see "Here's johnny!"
:::tip[Type-safe links]
Since you are using Typescript, you can benefit from using Wasp's type-safe `Link` component and the `routes` object. Check out the [type-safe links docs](https://wasp.sh/docs/advanced/links) for more details.
:::
### Cleaning Up
Now that you've seen how Wasp deals with Routes and Pages, it's finally time to build the Todo app.
Start by cleaning up the starter project and removing unnecessary code and files.
First, remove most of the code from the `MainPage` component:
```tsx title="src/MainPage.tsx"
export const MainPage = () => {
return
Hello world!
;
};
```
At this point, the main page should look like this:

You can now delete redundant files: `src/Main.css`, `src/assets/wasp-logo-rounded.svg`, and `src/HelloPage.tsx` (we won't need this page for the rest of the tutorial).
Since `src/HelloPage.tsx` no longer exists, remove its route from the `main.wasp.ts` file.
Your Wasp file should now look like this:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
export default app({
name: "TodoApp",
wasp: { version: "^0.25" },
title: "TodoApp",
head: [""],
spec: [
route("RootRoute", "/", page(MainPage)),
],
})
```
Excellent work!
You now have a basic understanding of Wasp and are ready to start building your TodoApp. We'll implement the app's core features in the following sections.
## Tutorial / 4. Database Entities
Entities are one of the most important concepts in Wasp and are how you define what gets stored in the database.
Wasp uses Prisma to talk to the database, and you define Entities by defining Prisma models in the `schema.prisma` file.
Since our Todo app is all about tasks, we'll define a Task entity by adding a Task model in the `schema.prisma` file:
```prisma title="schema.prisma"
// ...
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
}
```
:::note
Read more about how Wasp Entities work in the [Entities](https://wasp.sh/docs/data-model/entities) section or how Wasp uses the `schema.prisma` file in the [Prisma Schema File](https://wasp.sh/docs/data-model/prisma-file) section.
:::
To update the database schema to include this entity, stop the `wasp start` process, if it's running, and run:
```sh
wasp db migrate-dev
```
You'll need to do this any time you change an entity's definition. It instructs Prisma to create a new database migration and apply it to the database.
To take a look at the database and the new `Task` entity, run:
```sh
wasp db studio
```
This will open a new page in your browser to view and edit the data in your database.

Click on the `Task` entity and check out its fields! We don't have any data in our database yet, but we are about to change that.
## Tutorial / 5. Querying the Database
We want to know which tasks we need to do, so let's list them!
The primary way of working with Entities in Wasp is with [Queries and Actions](https://wasp.sh/docs/data-model/operations/overview), collectively known as ***Operations***.
Queries are used to read an entity, while Actions are used to create, modify, and delete entities. Since we want to list the tasks, we'll want to use a Query.
To list the tasks, you must:
1. Create a Query that fetches the tasks from the database.
2. Update the `MainPage.tsx` to use that Query and display the results.
### Defining the Query
We'll create a new Query called `getTasks`. We'll need to declare the Query in the Wasp file and write its implementation in TS.
#### Specifying a Query
We need to add a **query** specification to `main.wasp.ts` so that Wasp knows it exists:
```ts title="main.wasp.ts"
import { app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
export default app({
// ...
spec: [
route("RootRoute", "/", page(MainPage)),
// Tell Wasp that this query reads from the `Task` entity. Wasp will
// automatically update the results of this query when tasks are modified.
query(getTasks, { entities: ["Task"] }),
],
})
```
:::note
To generate the types used in the next section, make sure that `wasp start` is still running.
:::
#### Implementing a Query
Next, create a new file called `src/queries.ts` and define the TypeScript function we've just imported in our `query` spec:
```ts title="src/queries.ts"
import type { Task } from "wasp/entities";
import type { GetTasks } from "wasp/server/operations";
export const getTasks: GetTasks = async (args, context) => {
return context.entities.Task.findMany({
orderBy: { id: "asc" },
});
};
```
Wasp automatically generates the types `GetTasks` and `Task` based on the contents of `main.wasp.ts`:
- `Task` is a type corresponding to the `Task` entity you defined in `schema.prisma`.
- `GetTasks` is a generic type Wasp automatically generated based on the `getTasks` Query you defined in `main.wasp.ts`.
You can use these types to specify the Query's input and output types. This Query doesn't expect any arguments (its input type is `void`), but it does return an array of tasks (its output type is `Task[]`).
Annotating the Queries is optional, but highly recommended because doing so enables **full-stack type safety**. We'll see what this means in the next step.
Query function parameters:
- `args: object`
The arguments the caller passes to the Query.
- `context`
An object with extra information injected by Wasp. Its type depends on the Query specification.
Since the Query spec in `main.wasp.ts` says that the `getTasks` Query uses the `Task` entity, Wasp injected a [Prisma client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud) for the `Task` entity as `context.entities.Task` - we used it above to fetch all the tasks from the database.
:::info
Queries and Actions are NodeJS functions executed on the server.
:::
### Invoking the Query On the Frontend
While we implement Queries on the server, Wasp generates client-side functions that automatically take care of serialization, network calls, and cache invalidation, allowing you to call the server code like it's a regular function.
This makes it easy for us to use the `getTasks` Query we just created in our React component:
```tsx title="src/MainPage.tsx"
import type { Task } from "wasp/entities";
import { getTasks, useQuery } from "wasp/client/operations";
export const MainPage = () => {
const { data: tasks, isLoading, error } = useQuery(getTasks);
return (
);
};
```
Most of this code is regular React, the only exception being the three special `wasp` imports:
- `getTasks` - The client-side Query function Wasp generated based on the `getTasks` spec in `main.wasp.ts`.
- `useQuery` - Wasp's [useQuery](https://wasp.sh/docs/data-model/operations/queries#the-usequery-hook-1) React hook, which is based on [react-query](https://github.com/tannerlinsley/react-query)'s hook with the same name.
- `Task` - The type for the Task entity defined in `schema.prisma`.
Notice how you don't need to annotate the type of the Query's return value: Wasp uses the types you defined while implementing the Query for the generated client-side function. This is **full-stack type safety**: the types on the client always match the types on the server.
We could have called the Query directly using `getTasks()`, but the `useQuery` hook makes it reactive: React will re-render the component every time the Query changes. Remember that Wasp automatically refreshes Queries whenever the data is modified.
With these changes, you should be seeing the text "No tasks" on the screen:

We'll create a form to add tasks in the next step ๐ช
## Tutorial / 6. Modifying Data
In the previous section, you learned about using Queries to fetch data. Let's now learn about Actions so you can add and update tasks in the database.
In this section, you will create:
1. A Wasp Action that creates a new task.
2. A React form that calls that Action when the user creates a task.
### Creating a New Action
Creating an Action is very similar to creating a Query.
#### Specifying an Action
We must first declare the Action in `main.wasp.ts`:
```ts title="main.wasp.ts"
import { action, app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { createTask } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
route("RootRoute", "/", page(MainPage)),
query(getTasks, { entities: ["Task"] }),
action(createTask, { entities: ["Task"] }),
],
})
```
#### Implementing an Action
Let's now define a TypeScript function for our `createTask` Action:
```ts title="src/actions.ts"
import type { Task } from "wasp/entities";
import type { CreateTask } from "wasp/server/operations";
type CreateTaskPayload = Pick;
export const createTask: CreateTask = async (
args,
context,
) => {
return context.entities.Task.create({
data: { description: args.description },
});
};
```
Once again, we've annotated the Action with the `CreateTask` and `Task` types generated by Wasp. Just like with queries, defining the types on the implementation makes them available on the frontend, giving us **full-stack type safety**.
:::tip
We put the function in a new file `src/actions.ts`, but we could have put it anywhere we wanted! There are no limitations here, as long as the reference import in `main.wasp.ts` points to it and the file is located within the `src` directory.
:::
### Invoking the Action on the Client
Start by defining a form for creating new tasks.
```tsx title="src/MainPage.tsx"
import type { FormEvent } from "react";
import type { Task } from "wasp/entities";
import {
createTask,
getTasks,
useQuery,
} from "wasp/client/operations";
// ... MainPage, TaskView, TasksList ...
const NewTaskForm = () => {
const handleSubmit = async (event: FormEvent) => {
event.preventDefault();
try {
const target = event.target as HTMLFormElement;
const description = target.description.value;
target.reset();
await createTask({ description });
} catch (err: any) {
window.alert("Error: " + err.message);
}
};
return (
);
};
```
Unlike Queries, you can call Actions directly (without wrapping them in a hook) because they don't need reactivity. The rest is just regular React code.
Finally, because we've previously annotated the Action's server implementation with the correct type, Wasp knows that the `createTask` Action expects a value of type `{ description: string }` (try changing the argument and reading the error message). Wasp also knows that a call to the `createTask` Action returns a `Task` but we are not using it in this example.
All that's left now is adding this form to the page component:
```tsx title="src/MainPage.tsx"
import type { FormEvent } from "react";
import type { Task } from "wasp/entities";
import { createTask, getTasks, useQuery } from "wasp/client/operations";
export const MainPage = () => {
const { data: tasks, isLoading, error } = useQuery(getTasks);
return (
);
};
// ... TaskView, TasksList, NewTaskForm ...
```
Great work!
You now have a form for creating new tasks.
Try creating a "Build a Todo App in Wasp" task and see it appear in the list below. The task is created on the server and saved in the database.
Try refreshing the page or opening it in another browser. You'll see the tasks are still there!

:::note[Automatic Query Invalidation]
When you create a new task, the list of tasks is automatically updated to display the new task, even though you haven't written any code that does that! Wasp handles these automatic updates under the hood.
When you declared the `getTasks` and `createTask` operations, you specified that they both use the `Task` entity. So when `createTask` is called, Wasp knows that the data `getTasks` fetches may have changed and automatically updates it in the background. This means that **out of the box, Wasp keeps all your queries in sync with any changes made through Actions**.
This behavior is convenient as a default but can cause poor performance in large apps. While there is no mechanism for overriding this behavior yet, it is something that we plan to include in Wasp in the future. This feature is tracked [here](https://github.com/wasp-lang/wasp/issues/63).
:::
### A Second Action
Our Todo app isn't finished if you can't mark a task as done.
We'll create a new Action to update a task's status and call it from React whenever a task's checkbox is toggled.
Since we've already created one task together, try to create this one yourself. It should be an Action named `updateTask` that receives the task's `id` and its `isDone` status. You can see our implementation below.
Solution
Declaring the Action in `main.wasp.ts`:
```ts title="main.wasp.ts"
import { action, app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { createTask, updateTask } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
// ... existing routes and queries
action(createTask, { entities: ["Task"] }),
action(updateTask, { entities: ["Task"] }),
],
})
```
Implementing the Action on the server:
```ts title="src/actions.ts"
import type { Task } from "wasp/entities";
import type { CreateTask, UpdateTask } from "wasp/server/operations";
// ...
type UpdateTaskPayload = Pick;
export const updateTask: UpdateTask = async (
{ id, isDone },
context,
) => {
return context.entities.Task.update({
where: { id },
data: {
isDone: isDone,
},
});
};
```
You can now call `updateTask` from the React component:
```tsx title="src/MainPage.tsx"
import type { FormEvent, ChangeEvent } from "react";
import type { Task } from "wasp/entities";
import {
updateTask,
createTask,
getTasks,
useQuery,
} from "wasp/client/operations";
// ... MainPage ...
const TaskView = ({ task }: { task: Task }) => {
const handleIsDoneChange = async (event: ChangeEvent) => {
try {
await updateTask({
id: task.id,
isDone: event.target.checked,
});
} catch (error: any) {
window.alert("Error while updating task: " + error.message);
}
};
return (
{task.description}
);
};
// ... TasksList, NewTaskForm ...
```
Awesome! You can now mark this task as done.
It's time to make one final addition to your app: supporting multiple users.
## Tutorial / 7. Adding Authentication
Most modern apps need a way to create and authenticate users. Wasp makes this as easy as possible with its first-class auth support.
To add users to your app, you must:
- [ ] Create a `User` Entity.
- [ ] Tell Wasp to use the *Username and Password* authentication.
- [ ] Add login and signup pages.
- [ ] Update the main page to require authentication.
- [ ] Add a relation between `User` and `Task` entities.
- [ ] Modify your Queries and Actions so users can only see and modify their tasks.
- [ ] Add a logout button.
### Creating a User Entity
Since Wasp manages authentication, it will create [the auth related entities](https://wasp.sh/docs/auth/entities) for you in the background. Nothing to do here!
You must only add the `User` Entity to keep track of who owns which tasks:
```prisma title="schema.prisma"
// ...
model User {
id Int @id @default(autoincrement())
}
```
### Adding Auth to the Project
Next, tell Wasp to use full-stack [authentication](https://wasp.sh/docs/auth/overview):
```ts title="main.wasp.ts"
import { action, app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { createTask, updateTask } from "./src/actions" with { type: "ref" }
export default app({
// ...
auth: {
// Tells Wasp which entity to use for storing users.
userEntity: "User",
methods: {
// Enable username and password auth.
usernameAndPassword: {},
},
// We'll see how this is used in a bit.
onAuthFailedRedirectTo: "/login",
},
spec: [
route("RootRoute", "/", page(MainPage)),
query(getTasks, { entities: ["Task"] }),
action(createTask, { entities: ["Task"] }),
action(updateTask, { entities: ["Task"] }),
],
})
```
Don't forget to update the database schema by running:
```sh
wasp db migrate-dev
```
By doing this, Wasp will create:
- [Auth UI](https://wasp.sh/docs/auth/ui) with login and signup forms.
- A `logout()` action.
- A React hook `useAuth()`.
- `context.user` for use in Queries and Actions.
:::info
Wasp also supports authentication using [Google](https://wasp.sh/docs/auth/social-auth/google), [GitHub](https://wasp.sh/docs/auth/social-auth/github), and [email](https://wasp.sh/docs/auth/email), with more on the way!
:::
### Adding Login and Signup Pages
Wasp creates the login and signup forms for us, but we still need to define the pages to display those forms on. We'll start by declaring the pages in the Wasp file:
```ts title="main.wasp.ts"
import { action, app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { SignupPage } from "./src/SignupPage" with { type: "ref" }
import { LoginPage } from "./src/LoginPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { createTask, updateTask } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
route("RootRoute", "/", page(MainPage)),
route("SignupRoute", "/signup", page(SignupPage)),
route("LoginRoute", "/login", page(LoginPage)),
// ... existing queries and actions
],
})
```
Great, Wasp now knows these pages exist!
Here's the React code for the pages you've just imported:
```tsx title="src/LoginPage.tsx"
import { Link } from "react-router";
import { LoginForm } from "wasp/client/auth";
export const LoginPage = () => {
return (
I don't have an account yet (go to signup).
);
};
```
The signup page is very similar to the login page:
```tsx title="src/SignupPage.tsx"
import { Link } from "react-router";
import { SignupForm } from "wasp/client/auth";
export const SignupPage = () => {
return (
I already have an account (go to login).
);
};
```
:::tip[Type-safe links]
Since you are using Typescript, you can benefit from using Wasp's type-safe `Link` component and the `routes` object. Check out the [type-safe links docs](https://wasp.sh/docs/advanced/links) for more details.
:::
### Update the Main Page to Require Auth
We don't want users who are not logged in to access the main page, because they won't be able to create any tasks. So let's make the page private by requiring the user to be logged in:
```ts title="main.wasp.ts"
import { action, app, page, query, route } from "@wasp.sh/spec"
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { SignupPage } from "./src/SignupPage" with { type: "ref" }
import { LoginPage } from "./src/LoginPage" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { createTask, updateTask } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
route("RootRoute", "/", page(MainPage, {
authRequired: true,
})),
// ... existing routes, queries, and actions
],
})
```
Now that auth is required for this page, unauthenticated users will be redirected to `/login`, as we specified with `auth.onAuthFailedRedirectTo`.
Additionally, when `authRequired` is `true`, the page's React component will be provided a `user` object as prop.
```tsx title="src/MainPage.tsx"
import type { AuthUser } from "wasp/auth";
// ... existing imports
export const MainPage = ({ user }: { user: AuthUser }) => {
const { data: tasks, isLoading, error } = useQuery(getTasks);
// ...
};
```
Ok, time to test this out. Navigate to the main page (`/`) of the app. You'll get redirected to `/login`, where you'll be asked to authenticate.
Since we just added users, you don't have an account yet. Go to the signup page and create one. You'll be sent back to the main page where you will now be able to see the TODO list!
Let's check out what the database looks like. Start the Prisma Studio:
```shell
wasp db studio
```

You'll notice that we now have a `User` entity in the database alongside the `Task` entity.
However, you will notice that if you try logging in as different users and creating some tasks, all users share the same tasks. That's because you haven't yet updated the queries and actions to have per-user tasks. Let's do that next.
You might notice some extra Prisma models like `Auth`, `AuthIdentity` and `Session` that Wasp created for you. You don't need to care about these right now, but if you are curious, you can read more about them [here](https://wasp.sh/docs/auth/entities).
### Defining a User-Task Relation
First, let's define a one-to-many relation between users and tasks (check the [Prisma docs on relations](https://www.prisma.io/docs/orm/prisma-schema/data-model/relations)):
```prisma title="schema.prisma"
// ...
model User {
id Int @id @default(autoincrement())
tasks Task[]
}
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
user User? @relation(fields: [userId], references: [id])
userId Int?
}
```
As always, you must migrate the database after changing the Entities:
```sh
wasp db migrate-dev
```
:::note
We made `user` and `userId` in `Task` optional (via `?`) because that allows us to keep the existing tasks, which don't have a user assigned, in the database.
This isn't recommended because it allows an unwanted state in the database (what is the purpose of the task not belonging to anybody?) and normally we would not make these fields optional.
Instead, we would do a data migration to take care of those tasks, even if it means just deleting them all. However, for this tutorial, for the sake of simplicity, we will stick with this.
:::
### Updating Operations to Check Authentication
Next, let's update the queries and actions to forbid access to non-authenticated users and to operate only on the currently logged-in user's tasks:
```ts title="src/queries.ts"
import type { Task } from "wasp/entities";
import { HttpError } from "wasp/server";
import type { GetTasks } from "wasp/server/operations";
export const getTasks: GetTasks = async (args, context) => {
if (!context.user) {
throw new HttpError(401);
}
return context.entities.Task.findMany({
where: { user: { id: context.user.id } },
orderBy: { id: "asc" },
});
};
```
```ts title="src/actions.ts"
import type { Task } from "wasp/entities";
import { HttpError } from "wasp/server";
import type { CreateTask, UpdateTask } from "wasp/server/operations";
type CreateTaskPayload = Pick;
export const createTask: CreateTask = async (
args,
context,
) => {
if (!context.user) {
throw new HttpError(401);
}
return context.entities.Task.create({
data: {
description: args.description,
user: { connect: { id: context.user.id } },
},
});
};
type UpdateTaskPayload = Pick;
export const updateTask: UpdateTask<
UpdateTaskPayload,
{ count: number }
> = async (args, context) => {
if (!context.user) {
throw new HttpError(401);
}
return context.entities.Task.updateMany({
where: { id: args.id, user: { id: context.user.id } },
data: { isDone: args.isDone },
});
};
```
:::note
Due to how Prisma works, we had to convert `update` to `updateMany` in `updateTask` action to be able to specify the user id in `where`.
:::
With these changes, each user should have a list of tasks that only they can see and edit.
Try playing around, adding a few users and some tasks for each of them. Then open the DB studio:
```sh
wasp db studio
```

You will see that each user has their tasks, just as we specified in our code!
### Logout Button
Last, but not least, let's add the logout functionality:
```tsx title="src/MainPage.tsx"
import type { AuthUser } from "wasp/auth";
import { logout } from "wasp/client/auth";
// ... existing imports
export const MainPage = ({ user }: { user: AuthUser }) => {
// ...
return (
{/* ... */}
);
};
```
This is it, we have a working authentication system, and our Todo app is multi-user!
### What's Next?
We did it ๐ You've followed along with this tutorial to create a basic Todo app with Wasp.
You can find the complete code for the TS version of the tutorial [here](https://github.com/wasp-lang/wasp/tree/release/examples/tutorials/TodoAppTs).
You should be ready to learn about more complicated features and go more in-depth with the features already covered. Scroll through the sidebar on the left side of the page to see every feature Wasp has to offer. Or, let your imagination run wild and start building your app! โจ
Looking for inspiration?
- Get a jump start on your next project with [Starter Templates](https://wasp.sh/docs/project/starter-templates).
- Check out our [official examples](https://github.com/wasp-lang/wasp/tree/release/examples).
- Make a real-time app with [Web Sockets](https://wasp.sh/docs/advanced/web-sockets).
:::note
If you notice that some of the features you'd like to have are missing, or have any other kind of feedback, please write to us on [Discord](https://discord.gg/rzdnErX) or create an issue on [Github](https://github.com/wasp-lang/wasp), so we can learn which features to add/improve next ๐
If you would like to contribute or help to build a feature, let us know! You can find more details on contributing [here](https://wasp.sh/docs/contributing).
:::
Oh, and do [**subscribe to our newsletter**](https://wasp.sh/#signup)! We usually send one per month, and Matija does his best to unleash his creativity to make them engaging and fun to read \:D!
## Data Model / Entities
Entities are the foundation of your app's data model. In short, an Entity defines a model in your database.
Wasp uses the excellent [Prisma ORM](https://www.prisma.io/) to implement all database functionality and occasionally enhances it with a thin abstraction layer. This means that you use the `schema.prisma` file to define your database models and relationships. Wasp understands the Prisma schema file and picks up all the models you define there. You can read more about this in the [Prisma Schema File](https://wasp.sh/docs/data-model/prisma-file) section of the docs.
In your project, you'll find a `schema.prisma` file in the root directory:
```
.
โโโ main.wasp.ts
...
โโโ package.json
โโโ public
โโโ schema.prisma
โโโ src
โโโ tsconfig.json
โโโ tsconfig.src.json
โโโ tsconfig.wasp.json
โโโ vite.config.ts
```
Prisma uses the *Prisma Schema Language*, a simple definition language explicitly created for defining models. The language is declarative and very intuitive. We'll also go through an example later in the text, so there's no need to go and thoroughly learn it right away. Still, if you're curious, look no further than Prisma's official documentation:
- [Basic intro and examples](https://www.prisma.io/docs/orm/prisma-schema/overview)
- [A more exhaustive language specification](https://www.prisma.io/docs/orm/reference/prisma-schema-reference)
### Defining an Entity
A Prisma `model` declaration in the `schema.prisma` file represents a Wasp Entity.
Entity vs Model
You might wonder why we distinguish between a **Wasp Entity** and a **Prisma model** if they're essentially the same thing right now.
While defining a Prisma model is currently the only way to create an Entity in Wasp, the Entity concept is a higher-level abstraction. We plan to expand on Entities in the future, both in terms of how you can define them and what you can do with them.
So, think of an Entity as a Wasp concept and a model as a Prisma concept. For now, all Prisma models are Entities and vice versa, but this relationship might evolve as Wasp grows.
Here's how you could define an Entity that represents a Task:
```prisma title="schema.prisma"
model Task {
id String @id @default(uuid())
description String
isDone Boolean @default(false)
}
```
The above Prisma `model` definition tells Wasp to create a table for storing Tasks where each task has three fields (i.e., the `tasks` table has three columns):
- `id` - A string value serving as a primary key. The database automatically generates it by generating a random unique ID.
- `description` - A string value for storing the task's description.
- `isDone` - A boolean value indicating the task's completion status. If you don't set it when creating a new task, the database sets it to `false` by default.
Wasp also exposes a type for working with the created Entity. You can import and use it like this:
```ts
import { Task } from "wasp/entities"
const task: Task = { ... }
// You can also define functions for working with entities
function getInfoMessage(task: Task): string {
const isDoneText = task.isDone ? "is done" : "is not done"
return `Task '${task.description}' is ${isDoneText}.`
}
```
Using the `Task` type in `getInfoMessage`'s definition connects the argument's type with the `Task` entity.
This coupling removes duplication and ensures the function keeps the correct signature even if you change the entity. Of course, the function might throw type errors depending on how you change it, but that's precisely what you want!
Entity types are available everywhere, including the client code:
```ts
import { Task } from "wasp/entities"
export function ExamplePage() {
const task: Task = {
id: "some-uuid-1234",
description: "Some random task",
isDone: false,
}
return
{task.description}
}
```
The mentioned type safety mechanisms also apply here: changing the task entity in our `schema.prisma` file changes the imported type, which might throw a type error and warn us that our task definition is outdated.
You'll learn even more about Entity types when you start using [them with operations](#using-entities-in-operations).
#### Working with Entities
Let's see how you can define and work with Wasp Entities:
1. Create/update some Entities in the `schema.prisma` file.
2. Run `wasp db migrate-dev`. This command syncs the database model with the Entity definitions the `schema.prisma` file. It does this by creating migration scripts.
3. Migration scripts are automatically placed in the `migrations/` folder. Make sure to commit this folder into version control.
4. Use Wasp's JavaScript API to work with the database when implementing Operations (we'll cover this in detail when we talk about [operations](https://wasp.sh/docs/data-model/operations/overview)).
##### Using Entities in Operations
Most of the time, you will be working with Entities within the context of [Operations (Queries & Actions)](https://wasp.sh/docs/data-model/operations/overview). We'll see how that's done on the next page.
##### Using Entities directly
If you need more control, you can directly interact with Entities by importing and using the [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client/crud). We recommend sticking with conventional Wasp-provided mechanisms, only resorting to directly using the Prisma client only if you need a feature Wasp doesn't provide.
You can only use the Prisma Client in your Wasp server code. You can import it like this:
```ts
import { prisma } from "wasp/server"
prisma.task.create({
description: "Read the Entities doc",
isDone: true // almost :)
})
```
:::note[Available Prisma features in the client]
While the Prisma Client is not available in your client code, you can still import Prisma there, for accessing type definitions (notably, `enum`s).
You can see more information in the overview of [supported Prisma Schema features](https://wasp.sh/docs/data-model/prisma-file#the-enum-blocks).
:::
#### Next steps
Now that we've seen how to define Entities that represent Wasp's core data model, we'll see how to make the most of them in other parts of Wasp. Keep reading to learn all about Wasp Operations!
## Data Model / Operations / Overview
While Entities enable you to define your app's data model and relationships, Operations are all about working with this data.
There are two kinds of Operations: [Queries](https://wasp.sh/docs/data-model/operations/queries) and [Actions](https://wasp.sh/docs/data-model/operations/actions). As their names suggest, Queries are meant for reading data, and Actions are meant for changing it (either by updating existing entries or creating new ones).
Keep reading to find out all there is to know about Operations in Wasp.
## Data Model / Operations / Queries
We'll explain what Queries are and how to use them. If you're looking for a detailed API specification, skip ahead to the [API Reference](#api-reference).
You can use Queries to fetch data from the server. They shouldn't modify the server's state. Fetching all comments on a blog post, a list of users that liked a video, information about a single product based on its ID... All of these are perfect use cases for a Query.
:::tip
Queries are fairly similar to Actions in terms of their API. Therefore, if you're already familiar with Actions, you might find reading the entire guide repetitive.
We instead recommend skipping ahead and only reading [the differences between Queries and Actions](https://wasp.sh/docs/data-model/operations/actions#differences-between-queries-and-actions), and consulting the [API Reference](#api-reference) as needed.
:::
### Working with Queries
You declare queries in the Wasp file and implement them using NodeJS. Wasp not only runs these queries within the server's context but also creates code that enables you to call them from any part of your codebase, whether it's on the client or server side.
This means you don't have to build an HTTP API for your query, manage server-side request handling, or even deal with client-side response handling and caching. Instead, just concentrate on implementing the business logic inside your query, and let Wasp handle the rest!
To create a Query, you must:
1. Declare the Query in Wasp using the `query` spec.
2. Define the Query's NodeJS implementation.
After completing these two steps, you'll be able to use the Query from any point in your code.
#### Specifying Queries
To create a Query in Wasp, we begin with a `query` spec.
Let's declare two Queries - one to fetch all tasks, and another to fetch tasks based on a filter, such as whether a task is done:
```ts title="main.wasp.ts"
import { app, query } from "@wasp.sh/spec"
import { getAllTasks, getFilteredTasks } from "./src/queries" with { type: "ref" }
export default app({
// ...
spec: [
query(getAllTasks),
query(getFilteredTasks),
],
})
```
If you want to know about all supported options for the `query` spec, take a look at the [API Reference](#api-reference).
:::note
When `main.wasp.ts` needs to point to your code, it uses imports like this:
```ts
import { MainPage } from "./src/MainPage" with { type: "ref" }
```
Notice the `with { type: "ref" }` part at the end of the import statement. This tells Wasp to treat the import as a reference to your app's code, without running the imported code. For more details and examples, see [reference imports](https://wasp.sh/docs/general/spec#reference-imports).
:::
:::info
You might have noticed that we told Wasp to import Query implementations that don't yet exist. Don't worry about that for now. We'll write the implementations imported from `queries.ts` in the next section.
It's a good idea to start with the high-level concept (the Query spec in the Wasp file) and only then deal with the implementation details (the Query's implementation in JavaScript).
:::
After declaring a Wasp Query, Wasp derives the Query's name from the function you pass to `query`. For example, `query(getFilteredTasks)` creates a Query named `getFilteredTasks`.
Two important things then happen:
- Wasp **generates a server-side NodeJS function** with the Query's name.
- Wasp **generates a client-side JavaScript function** with the Query's name (e.g., `getFilteredTasks`). This function takes a single optional argument - an object containing any serializable data you wish to use inside the Query. Wasp will send this object over the network and pass it into the Query's implementation as its first positional argument (more on this when we look at the implementations). Such an abstraction works thanks to an HTTP API route handler Wasp generates on the server, which calls the Query's NodeJS implementation under the hood.
Generating these two functions ensures a similar calling interface across the entire app (both client and server).
#### Implementing Queries in Node
Now that we've declared the Query, what remains is to implement it. We've instructed Wasp to look for the Queries' implementations in the file `src/queries.ts`, so that's where we should export them from.
Here's how you might implement the previously declared Queries `getAllTasks` and `getFilteredTasks`:
```ts title="src/queries.ts"
import { type GetAllTasks, type GetFilteredTasks } from "wasp/server/operations"
type Task = {
id: number
description: string
isDone: boolean
}
// our "database"
const tasks: Task[] = [
{ id: 1, description: "Buy some eggs", isDone: true },
{ id: 2, description: "Make an omelette", isDone: false },
{ id: 3, description: "Eat breakfast", isDone: false },
]
// You don't need to use the arguments if you don't need them
export const getAllTasks: GetAllTasks = () => {
return tasks
}
// The 'args' object is something sent by the caller (most often from the client)
export const getFilteredTasks: GetFilteredTasks<
Pick,
Task[]
> = (args) => {
const { isDone } = args
return tasks.filter((task) => task.isDone === isDone)
}
```
:::info[Payload constraints]
Wasp uses [superjson](https://github.com/flightcontrolhq/superjson) under the hood. This means you're not limited to only sending and receiving JSON payloads.
Wasp will automatically handle the serialization and deserialization [for all the data types that superjson supports](https://github.com/flightcontrolhq/superjson#decimaljs--prismadecimal:~\:text=Superjson%20supports%20many%20extra%20types) (like `biging`, `Date`, `Map`, `Set`, etc.), and for [Prisma.Decimal](https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types#working-with-decimal).
As long as you're annotating your Operations with the correct automatically generated types, TypeScript ensures your payloads are valid (i.e., Wasp knows how to serialize and deserialize them).
:::
##### Type support for Queries
Wasp automatically generates the types `GetAllTasks` and `GetFilteredTasks` based on your Wasp file's specs:
- `GetAllTasks` is a generic type automatically generated by Wasp, based on the Query spec for `getAllTasks`.
- `GetFilteredTasks` is also a generic type automatically generated by Wasp, based on the Query spec for `getFilteredTasks`.
Use these types to type the Query's implementation. It's optional but very helpful since doing so properly types the Query's context.
In this case, TypeScript will know the `context.entities` object must include the `Task` entity. TypeScript also knows whether the `context` object includes user information (it depends on whether your Query uses auth).
The generated types are generic and accept two optional type arguments: `Input` and `Output`.
1. `Input` - The argument (the payload) received by the Query function.
2. `Output` - The Query function's return type.
Use these type arguments to type the Query's inputs and outputs.
Explanation for the example above
The above code says that the Query `getAllTasks` doesn't expect any arguments (its input type is `void`), but it does return a list of tasks (its output type is `Task[]`).
On the other hand, the Query `getFilteredTasks` expects an object of type `{ isDone: boolean }`. This type is derived from the `Task` entity type.
If you don't care about typing the Query's inputs and outputs, you can omit both type arguments. TypeScript will then infer the most general types (`never` for the input and `unknown` for the output).
Specifying `Input` or `Output` is completely optional, but we highly recommended it. Doing so gives you:
- Type support for the arguments and the return value inside the implementation.
- **Full-stack type safety**. We'll explore what this means when we discuss calling the Query from the client.
Read more about type support for implementing Queries in the [API Reference](#implementing-queries).
:::tip[Inferring the return type]
If don't want to explicitly type the Query's return value, the `satisfies` keyword tells TypeScript to infer it automatically:
```typescript
const getFoo = (async (_args, context) => {
const foos = await context.entities.Foo.findMany()
return {
foos,
message: "Here are some foos!",
queriedAt: new Date(),
}
}) satisfies GetFoo
```
From the snippet above, TypeScript knows:
1. The correct type for `context`.
2. The Query's return type is `{ foos: Foo[], message: string, queriedAt: Date }`.
If you don't need the context, you can skip specifying the Query's type (and arguments):
```typescript
const getFoo = () => ({ name: "Foo", date: new Date() })
```
:::
For a detailed explanation of the Query definition API (more precisely, its arguments and return values), check the [API Reference](#api-reference).
#### Using Queries
##### Using Queries on the client
To call a Query on the client, you can import it from `wasp/client/operations` and call it directly.
The usage doesn't change depending on whether the Query is authenticated or not. Wasp authenticates the logged-in user in the background.
```ts
import { getAllTasks, getFilteredTasks } from "wasp/client/operations"
// TypeScript automatically infers the return values and type-checks
// the payloads.
const allTasks = await getAllTasks()
const doneTasks = await getFilteredTasks({ isDone: true })
```
Wasp supports **automatic full-stack type safety**. You only need to specify the Query's type in its server-side definition, and the client code will automatically know its API payload types.
##### Using Queries on the server
Calling a Query on the server is similar to calling it on the client.
Here's what you have to do differently:
- Import Queries from `wasp/server/operations` instead of `wasp/client/operations`.
- Make sure you pass in a `context` object with the `user` field to authenticated Queries.
- Note that you don't have to pass other parts of the `context` object, like Entities, those will get injected automatically.
```ts
import { getAllTasks, getFilteredTasks } from "wasp/server/operations"
const user = // Get an AuthUser object, e.g., from context.user in an operation.
// TypeScript automatically infers the return values and type-checks
// the payloads.
const allTasks = await getAllTasks({ user })
const doneTasks = await getFilteredTasks({ isDone: true }, { user })
```
##### The `useQuery` hook
When using Queries on the client, you can make them reactive with the `useQuery` hook. This hook comes bundled with Wasp and is a thin wrapper around the `useQuery` hook from [*react-query*](https://github.com/tannerlinsley/react-query). The only difference is that you don't need to supply the key - Wasp handles this for you automatically.
Here's an example of calling the Queries using the `useQuery` hook:
```tsx title="src/MainPage.tsx"
import React from "react"
import { type Task } from "wasp/entities"
import { useQuery, getAllTasks, getFilteredTasks } from "wasp/client/operations"
const MainPage = () => {
// TypeScript automatically infers return values and type-checks payload types.
const { data: allTasks, error: error1 } = useQuery(getAllTasks)
const { data: doneTasks, error: error2 } = useQuery(getFilteredTasks, {
isDone: true,
})
if (error1 !== null || error2 !== null) {
return
)
}
export default MainPage
```
Notice how you don't need to annotate the Query's return value type. Wasp automatically infers the from the Query's backend implementation. This is **full-stack type safety**: the types on the client always match the types on the server.
For a detailed specification of the `useQuery` hook, check the [API Reference](#api-reference).
#### Error Handling
For security reasons, all exceptions thrown in the Query's NodeJS implementation are sent to the client as responses with the HTTP status code `500`, with all other details removed. Hiding error details by default helps against accidentally leaking possibly sensitive information over the network.
If you do want to pass additional error information to the client, you can construct and throw an appropriate `HttpError` in your implementation:
```ts title="src/queries.ts"
import { type GetAllTasks } from "wasp/server/operations"
import { HttpError } from "wasp/server"
export const getAllTasks: GetAllTasks = async (args, context) => {
throw new HttpError(
403, // status code
"You can't do this!", // message
{ foo: "bar" } // data
)
}
```
If the status code is `4xx`, the client will receive a response object with the corresponding `message` and `data` fields, and it will rethrow the error (including these fields). To prevent information leakage, the server won't forward these fields for any other HTTP status codes.
#### Using Entities in Queries
In most cases, resources used in Queries will be [Entities](https://wasp.sh/docs/data-model/entities). To use an Entity in your Query, add it to the `query` spec in Wasp:
```ts title="main.wasp.ts"
import { app, query } from "@wasp.sh/spec"
import { getAllTasks, getFilteredTasks } from "./src/queries" with { type: "ref" }
export default app({
// ...
spec: [
query(getAllTasks, { entities: ["Task"] }),
query(getFilteredTasks, { entities: ["Task"] }),
],
})
```
Wasp will inject the specified Entity into the Query's `context` argument, giving you access to the Entity's Prisma API:
```ts title="src/queries.ts"
import { type Task } from "wasp/entities"
import { type GetAllTasks, type GetFilteredTasks } from "wasp/server/operations"
export const getAllTasks: GetAllTasks = async (args, context) => {
return context.entities.Task.findMany({})
}
export const getFilteredTasks: GetFilteredTasks<
Pick,
Task[]
> = async (args, context) => {
return context.entities.Task.findMany({
where: { isDone: args.isDone },
})
}
```
Again, annotating the Queries is optional, but greatly improves **full-stack type safety**.
The object `context.entities.Task` exposes `prisma.task` from [Prisma's CRUD API](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud).
### API Reference
#### Specifying Queries
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/query)
#### [query ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/query)
[All the options for declaring a query in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/query)
Declaring a Query enables you to import and use it anywhere in your code (on the server or the client). For example, for a Query that we declared as `getFoo`, Wasp generates two functions with the same name that you can import and use:
```ts
// Use it on the client
import { getFoo } from "wasp/client/operations"
// Use it on the server
import { getFoo } from "wasp/server/operations"
```
It also creates a type you can import on the server:
```ts
import { type GetFoo } from "wasp/server/operations"
```
#### Implementing Queries
The Query's implementation is a NodeJS function that takes two arguments (it can be an `async` function if you need to use the `await` keyword). Since both arguments are positional, you can name the parameters however you want, but we'll stick with `args` and `context`:
1. `args` (type depends on the Query)
An object containing the data **passed in when calling the query** (e.g., filtering conditions). Check [the usage examples](#using-queries) to see how to pass this object to the Query.
2. `context` (type depends on the Query)
An additional context object **passed into the Query by Wasp**. This object contains user session information, as well as information about entities. Check the [section about using entities in Queries](#using-entities-in-queries) to see how to use the entities field on the `context` object, or the [auth section](https://wasp.sh/docs/auth/overview#using-the-contextuser-object) to see how to use the `user` object.
After you [declare the query](#specifying-queries), Wasp generates a generic type you can use when defining its implementation. For the Query declared as `getSomething`, the generated type is called `GetSomething`:
```ts
import { type GetSomething } from "wasp/server/operations"
```
It expects two (optional) type arguments:
1. `Input`
The type of the `args` object (the Query's input payload). The default value is `never`.
2. `Output`
The type of the Query's return value (the Query's output payload). The default value is `unknown`.
The defaults were chosen to make the type signature as permissive as possible. If don't want your Query to take/return anything, use `void` as a type argument.
##### Example
The following Query:
```ts
import { app, query } from "@wasp.sh/spec"
import { getFoo } from "./src/queries" with { type: "ref" }
export default app({
// ...
spec: [
query(getFoo, { entities: ["Foo"] }),
],
})
```
Expects to find a named export `getFoo` from the file `src/queries.ts`
You can use the generated type `GetFoo` and specify the Query's inputs and outputs using its type arguments.
```ts title="src/queries.ts"
import { type GetFoo } from "wasp/server/operations"
type Foo = // ...
export const getFoo: GetFoo<{ id: number }, Foo> = (args, context) => {
// implementation
};
```
In this case, the Query expects to receive an object with an `id` field of type `number` (this is the type of `args`), and return a value of type `Foo` (this must match the type of the Query's return value).
#### The `useQuery` Hook
Wasp's `useQuery` hook is a thin wrapper around the `useQuery` hook from [*react-query*](https://github.com/tannerlinsley/react-query). One key difference is that Wasp doesn't expect you to supply the cache key - it takes care of it under the hood.
Wasp's `useQuery` hook accepts three arguments:
- `queryFn` required
The client-side query function generated by Wasp based on a `query` spec in your Wasp file.
- `queryFnArgs`
The arguments object (payload) you wish to pass into the Query. The Query's NodeJS implementation will receive this object as its first positional argument.
- `options`
A *react-query* `options` object. Use this to change [the default behavior](https://react-query.tanstack.com/guides/important-defaults) for this particular Query. If you want to change the global defaults, you can do so in the [client setup function](https://wasp.sh/docs/project/client-config#overriding-default-behaviour-for-queries).
For an example of usage, check [this section](#the-usequery-hook).
## Data Model / Operations / Actions
We'll explain what Actions are and how to use them. If you're looking for a detailed API specification, skip ahead to the [API Reference](#api-reference).
Actions are quite similar to [Queries](https://wasp.sh/docs/data-model/operations/queries), but with a key distinction: Actions are designed to modify and add data, while Queries are solely for reading data. Examples of Actions include adding a comment to a blog post, liking a video, or updating a product's price.
Actions and Queries work together to keep data caches up-to-date.
:::tip
Actions are almost identical to Queries in terms of their API. Therefore, if you're already familiar with Queries, you might find reading the entire guide repetitive.
We instead recommend skipping ahead and only reading [the differences between Queries and Actions](#differences-between-queries-and-actions), and consulting the [API Reference](#api-reference) as needed.
:::
### Working with Actions
Actions are declared in Wasp and implemented in NodeJS. Wasp runs Actions within the server's context, but it also generates code that allows you to call them from anywhere in your code (either client or server) using the same interface.
This means you don't have to worry about building an HTTP API for the Action, managing server-side request handling, or even dealing with client-side response handling and caching. Instead, just focus on developing the business logic inside your Action, and let Wasp handle the rest!
To create an Action, you need to:
1. Declare the Action in Wasp using the `action` spec.
2. Implement the Action's NodeJS functionality.
Once these two steps are completed, you can use the Action from anywhere in your code.
#### Specifying Actions
To create an Action in Wasp, we begin with an `action` spec. Let's declare two Actions - one for creating a task, and another for marking tasks as done:
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { createTask, markTaskAsDone } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
action(createTask),
action(markTaskAsDone),
],
})
```
If you want to know about all supported options for the `action` spec, take a look at the [API Reference](#api-reference).
:::note
When `main.wasp.ts` needs to point to your code, it uses imports like this:
```ts
import { MainPage } from "./src/MainPage" with { type: "ref" }
```
Notice the `with { type: "ref" }` part at the end of the import statement. This tells Wasp to treat the import as a reference to your app's code, without running the imported code. For more details and examples, see [reference imports](https://wasp.sh/docs/general/spec#reference-imports).
:::
:::info
You might have noticed that we told Wasp to import Action implementations that don't yet exist. Don't worry about that for now. We'll write the implementations imported from `actions.ts` in the next section.
It's a good idea to start with the high-level concept (the Action spec in the Wasp file) and only then deal with the implementation details (the Action's implementation in JavaScript).
:::
After declaring a Wasp Action, Wasp derives the Action's name from the function you pass to `action`. For example, `action(markTaskAsDone)` creates an Action named `markTaskAsDone`.
Two important things then happen:
- Wasp **generates a server-side NodeJS function** with the Action's name.
- Wasp **generates a client-side JavaScript function** with the Action's name (e.g., `markTaskAsDone`). This function takes a single optional argument - an object containing any serializable data you wish to use inside the Action. Wasp will send this object over the network and pass it into the Action's implementation as its first positional argument (more on this when we look at the implementations). Such an abstraction works thanks to an HTTP API route handler Wasp generates on the server, which calls the Action's NodeJS implementation under the hood.
Generating these two functions ensures a similar calling interface across the entire app (both client and server).
#### Implementing Actions in Node
Now that we've declared the Action, what remains is to implement it. We've instructed Wasp to look for the Actions' implementations in the file `src/actions.ts`, so that's where we should export them from.
Here's how you might implement the previously declared Actions `createTask` and `markTaskAsDone`:
```ts title="src/actions.ts"
import { type CreateTask, type MarkTaskAsDone } from "wasp/server/operations"
type Task = {
id: number
description: string
isDone: boolean
}
// our "database"
let nextId = 4
const tasks = [
{ id: 1, description: "Buy some eggs", isDone: true },
{ id: 2, description: "Make an omelette", isDone: false },
{ id: 3, description: "Eat breakfast", isDone: false },
]
// You don't need to use the arguments if you don't need them
export const createTask: CreateTask, Task> = (
args
) => {
const newTask = {
id: nextId,
isDone: false,
description: args.description,
}
nextId += 1
tasks.push(newTask)
return newTask
}
// The 'args' object is something sent by the caller (most often from the client)
export const markTaskAsDone: MarkTaskAsDone, void> = (
args
) => {
const task = tasks.find((task) => task.id === args.id)
if (!task) {
// We'll show how to properly handle such errors later
return
}
task.isDone = true
}
```
:::info[Payload constraints]
Wasp uses [superjson](https://github.com/flightcontrolhq/superjson) under the hood. This means you're not limited to only sending and receiving JSON payloads.
Wasp will automatically handle the serialization and deserialization [for all the data types that superjson supports](https://github.com/flightcontrolhq/superjson#decimaljs--prismadecimal:~\:text=Superjson%20supports%20many%20extra%20types) (like `biging`, `Date`, `Map`, `Set`, etc.), and for [Prisma.Decimal](https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types#working-with-decimal).
As long as you're annotating your Operations with the correct automatically generated types, TypeScript ensures your payloads are valid (i.e., Wasp knows how to serialize and deserialize them).
:::
##### Type support for Actions
Wasp automatically generates the types `CreateTask` and `MarkTaskAsDone` based on the specs in your Wasp file:
- `CreateTask` is a generic type that Wasp automatically generated based on the Action spec for `createTask`.
- `MarkTaskAsDone` is a generic type that Wasp automatically generated based on the Action spec for `markTaskAsDone`.
Use these types to type the Action's implementation. It's optional but very helpful since doing so properly types the Action's context.
In this case, TypeScript will know the `context.entities` object must include the `Task` entity. TypeScript also knows whether the `context` object includes user information (it depends on whether your Action uses auth).
The generated types are generic and accept two optional type arguments: `Input` and `Output`.
1. `Input` - The argument (the payload) received by the Action function.
2. `Output` - The Action function's return type.
Use these type arguments to type the Action's inputs and outputs.
Explanation for the example above
The above code says that the Action `createTask` expects an object with the new task's description (its input type is `Pick`) and returns the new task (its output type is `Task`).
On the other hand, the Action `markTaskAsDone` expects an object of type `Pick`. This type is derived from the `Task` entity type.
If you don't care about typing the Action's inputs and outputs, you can omit both type arguments. TypeScript will then infer the most general types (`never` for the input and `unknown` for the output).
Specifying `Input` or `Output` is completely optional, but we highly recommended it. Doing so gives you:
- Type support for the arguments and the return value inside the implementation.
- **Full-stack type safety**. We'll explore what this means when we discuss calling the Action from the client.
Read more about type support for implementing Actions in the [API Reference](#implementing-actions).
:::tip[Inferring the return type]
If don't want to explicitly type the Action's return value, the `satisfies` keyword tells TypeScript to infer it automatically:
```typescript
const createFoo = (async (_args, context) => {
const foo = await context.entities.Foo.create()
return {
newFoo: foo,
message: "Here's your foo!",
returnedAt: new Date(),
}
}) satisfies CreateFoo
```
From the snippet above, TypeScript knows:
1. The correct type for `context`.
2. The Action's return type is `{ newFoo: Foo, message: string, returnedAt: Date }`.
If you don't need the context, you can skip specifying the Action's type (and arguments):
```typescript
const createFoo = () => ({ name: "Foo", date: new Date() })
```
:::
For a detailed explanation of the Action definition API (more precisely, its arguments and return values), check the [API Reference](#api-reference).
#### Using Actions
##### Using Actions on the client
To call an Action on the client, you can import it from `wasp/client/operations` and call it directly.
The usage doesn't depend on whether the Action is authenticated or not. Wasp authenticates the logged-in user in the background.
```ts
import { createTask, markTaskAsDone } from "wasp/client/operations"
// TypeScript automatically infers the return values and type-checks
// the payloads.
const newTask = await createTask({ description: "Keep learning TypeScript" })
await markTaskAsDone({ id: 1 })
```
Wasp supports **automatic full-stack type safety**. You only need to specify the Action's type in its server-side definition, and the client code will automatically know its API payload types.
When using Actions on the client, you'll most likely want to use them inside a component:
```tsx title="src/pages/Task.tsx"
import React from "react"
import { useQuery, getTask, markTaskAsDone } from "wasp/client/operations"
export const TaskPage = ({ id }: { id: number }) => {
const { data: task } = useQuery(getTask, { id })
if (!task) {
return
"Loading"
}
const { description, isDone } = task
return (
Description:
{description}
Is done:
{isDone ? "Yes" : "No"}
{isDone || (
)}
)
}
```
Since Actions don't require reactivity, they are safe to use inside components without a hook. Still, Wasp provides comes with the `useAction` hook you can use to enhance actions. Read all about it in the [API Reference](#api-reference).
##### Using Actions on the server
Calling an Action on the server is similar to calling it on the client.
Here's what you have to do differently:
- Import Actions from `wasp/server/operations` instead of `wasp/client/operations`.
- Make sure you pass in a context object with the user to authenticated Actions.
```ts
import { createTask, markTaskAsDone } from "wasp/server/operations"
const user = // Get an AuthUser object, e.g., from context.user
// TypeScript automatically infers the return values and type-checks
// the payloads.
const newTask = await createTask(
{ description: "Keep learning TypeScript" },
{ user },
)
await markTaskAsDone({ id: 1 }, { user })
```
#### Error Handling
For security reasons, all exceptions thrown in the Action's NodeJS implementation are sent to the client as responses with the HTTP status code `500`, with all other details removed. Hiding error details by default helps against accidentally leaking possibly sensitive information over the network.
If you do want to pass additional error information to the client, you can construct and throw an appropriate `HttpError` in your implementation:
```ts title="src/actions.ts"
import { type CreateTask } from "wasp/server/operations"
import { HttpError } from "wasp/server"
export const createTask: CreateTask = async (args, context) => {
throw new HttpError(
403, // status code
"You can't do this!", // message
{ foo: "bar" } // data
)
}
```
#### Using Entities in Actions
In most cases, resources used in Actions will be [Entities](https://wasp.sh/docs/data-model/entities). To use an Entity in your Action, add it to the `action` spec in Wasp:
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { createTask, markTaskAsDone } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
action(createTask, { entities: ["Task"] }),
action(markTaskAsDone, { entities: ["Task"] }),
],
})
```
Wasp will inject the specified Entity into the Action's `context` argument, giving you access to the Entity's Prisma API. Wasp invalidates frontend Query caches by looking at the Entities used by each Action/Query. Read more about Wasp's smart cache invalidation [here](#cache-invalidation).
```ts title="src/actions.ts"
import { type CreateTask, type MarkTaskAsDone } from "wasp/server/operations"
import { type Task } from "wasp/entities"
// The 'args' object is the payload sent by the caller (most often from the client)
export const createTask: CreateTask, Task> = async (
args,
context
) => {
const newTask = await context.entities.Task.create({
data: {
description: args.description,
isDone: false,
},
})
return newTask
}
export const markTaskAsDone: MarkTaskAsDone, void> = async (
args,
context
) => {
await context.entities.Task.update({
where: { id: args.id },
data: { isDone: true },
})
}
```
Again, annotating the Actions is optional, but greatly improves **full-stack type safety**.
The object `context.entities.Task` exposes `prisma.task` from [Prisma's CRUD API](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud).
### Cache Invalidation
One of the trickiest parts of managing a web app's state is making sure the data returned by the Queries is up to date. Since Wasp uses *react-query* for Query management, we must make sure to invalidate Queries (more specifically, their cached results managed by *react-query*) whenever they become stale.
It's possible to invalidate the caches manually through several mechanisms *react-query* provides (e.g., refetch, direct invalidation). However, since manual cache invalidation quickly becomes complex and error-prone, Wasp offers a faster and a more effective solution to get you started: **automatic Entity-based Query cache invalidation**. Because Actions can (and most often do) modify the state while Queries read it, Wasp invalidates a Query's cache whenever an Action that uses the same Entity is executed.
For example, if the Action `createTask` and Query `getTasks` both use the Entity `Task`, executing `createTask` may cause the cached result of `getTasks` to become outdated. In response, Wasp will invalidate it, causing `getTasks` to refetch data from the server and update it.
In practice, this means that Wasp keeps the Queries "fresh" without requiring you to think about cache invalidation.
On the other hand, this kind of automatic cache invalidation can become wasteful (some updates might not be necessary) and will only work for Entities. If that's an issue, you can use the mechanisms provided by *react-query* for now, and expect more direct support in Wasp for handling those use cases in a nice, elegant way.
If you wish to optimistically set cache values after performing an Action, you can do so using [optimistic updates](https://stackoverflow.com/a/33009713). Configure them using Wasp's [useAction hook](#the-useaction-hook-and-optimistic-updates). This is currently the only manual cache invalidation mechanism Wasps supports natively. For everything else, you can always rely on *react-query*.
### Differences Between Queries and Actions
Actions and Queries are two closely related concepts in Wasp. They might seem to perform similar tasks, but Wasp treats them differently, and each concept represents a different thing.
Here are the key differences between Queries and Actions:
1. Actions can (and often should) modify the server's state, while Queries are only permitted to read it. Wasp relies on you adhering to this convention when performing cache invalidations, so it's crucial to follow it.
2. Actions don't need to be reactive, so you can call them directly. However, Wasp does provide a [`useAction` React hook](#the-useaction-hook-and-optimistic-updates) for adding extra behavior to the Action (like optimistic updates).
3. `action` specs in Wasp are mostly identical to `query` specs. The only difference lies in the spec's name.
### API Reference
#### Specifying Actions in Wasp Spec
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/action)
#### [action ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/action)
[All the options for declaring an action in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/action)
Declaring an Action enables you to import and use it anywhere in your code (on the server or the client). For example, for an Action that we declared as `createFoo`, Wasp generates two functions with the same name that you can import and use:
```ts
// Use it on the client
import { createFoo } from "wasp/client/operations"
// Use it on the server
import { createFoo } from "wasp/server/operations"
```
It also creates a type you can import on the server:
```ts
import { type CreateFoo } from "wasp/server/operations"
```
#### Implementing Actions
The Action's implementation is a NodeJS function that takes two arguments (it can be an `async` function if you need to use the `await` keyword). Since both arguments are positional, you can name the parameters however you want, but we'll stick with `args` and `context`:
1. `args` (type depends on the Action)
An object containing the data **passed in when calling the Action** (e.g., filtering conditions). Check [the usage examples](#using-actions) to see how to pass this object to the Action.
2. `context` (type depends on the Action)
An additional context object **passed into the Action by Wasp**. This object contains user session information, as well as information about entities. Check the [section about using entities in Actions](#using-entities-in-actions) to see how to use the entities field on the `context` object, or the [auth section](https://wasp.sh/docs/auth/overview#using-the-contextuser-object) to see how to use the `user` object.
After you [declare the Action](#specifying-actions), Wasp generates a generic type you can use when defining its implementation. For the Action declared as `createSomething`, the generated type is called `CreateSomething`:
```ts
import { type CreateSomething } from "wasp/server/operations"
```
It expects two (optional) type arguments:
1. `Input`
The type of the `args` object (the Action's input payload). The default value is `never`.
2. `Output`
The type of the Action's return value (the Action's output payload). The default value is `unknown`.
The defaults were chosen to make the type signature as permissive as possible. If don't want your Action to take/return anything, use `void` as a type argument.
##### Example
The following Action:
```ts
import { action, app } from "@wasp.sh/spec"
import { createFoo } from "./src/actions" with { type: "ref" }
export default app({
// ...
spec: [
action(createFoo, { entities: ["Foo"] }),
],
})
```
Expects to find a named export `createFoo` from the file `src/actions.ts`
You can use the generated type `CreateFoo` and specify the Action's inputs and outputs using its type arguments.
```ts title="src/actions.ts"
import { type CreateFoo } from "wasp/server/operations"
type Foo = // ...
export const createFoo: CreateFoo<{ bar: string }, Foo> = (args, context) => {
// implementation
};
```
In this case, the Action expects to receive an object with a `bar` field of type `string` (this is the type of `args`), and return a value of type `Foo` (this must match the type of the Action's return value).
#### The `useAction` Hook and Optimistic Updates
Make sure you understand how [Queries](https://wasp.sh/docs/data-model/operations/queries) and [Cache Invalidation](#cache-invalidation) work before reading this chapter.
When using Actions in components, you can enhance them with the help of the `useAction` hook. This hook comes bundled with Wasp, and is used for decorating Wasp Actions. In other words, the hook returns a function whose API matches the original Action while also doing something extra under the hood (depending on how you configure it).
The `useAction` hook accepts two arguments:
- `actionFn` required
The Wasp Action (the client-side Action function generated by Wasp based on an Action spec) you wish to enhance.
- `actionOptions`
An object configuring the extra features you want to add to the given Action. While this argument is technically optional, there is no point in using the `useAction` hook without providing it (it would be the same as using the Action directly). The Action options object supports the following fields:
- `optimisticUpdates`
An array of objects where each object defines an [optimistic update](https://stackoverflow.com/a/33009713) to perform on the Query cache. To define an optimistic update, you must specify the following properties:
- `getQuerySpecifier` required
A function returning the Query specifier (a value used to address the Query you want to update). A Query specifier is an array specifying the query function and arguments. For example, to optimistically update the Query used with `useQuery(fetchFilteredTasks, {isDone: true }]`, your `getQuerySpecifier` function would have to return the array `[fetchFilteredTasks, { isDone: true}]`. Wasp will forward the argument you pass into the decorated Action to this function (you can use the properties of the added/changed item to address the Query).
- `updateQuery` required
The function used to perform the optimistic update. It should return the desired state of the cache. Wasp will call it with the following arguments:
- `item` - The argument you pass into the decorated Action.
- `oldData` - The currently cached value for the Query identified by the specifier.
:::caution
The `updateQuery` function must be a pure function. It must return the desired cache value identified by the `getQuerySpecifier` function and *must not* perform any side effects.
Also, make sure you only update the Query caches affected by your Action causing the optimistic update (Wasp cannot yet verify this).
Finally, your implementation of the `updateQuery` function should work correctly regardless of the state of `oldData` (e.g., don't rely on array positioning). If you need to do something else during your optimistic update, you can directly use *react-query*'s lower-level API (read more about it [here](#advanced-usage)).
:::
Here's an example showing how to configure the Action `markTaskAsDone` that toggles a task's `isDone` status to perform an optimistic update:
```tsx title="src/pages/Task.tsx"
import React from "react"
import {
useQuery,
useAction,
type OptimisticUpdateDefinition,
getTask,
markTaskAsDone,
} from "wasp/client/operations"
type TaskPayload = Pick;
const TaskPage = ({ id }: { id: number }) => {
const { data: task } = useQuery(getTask, { id });
// Typescript automatically type-checks the payload type.
const markTaskAsDoneOptimistically = useAction(markTaskAsDone, {
optimisticUpdates: [
{
getQuerySpecifier: ({ id }) => [getTask, { id }],
updateQuery: (_payload, oldData) => ({ ...oldData, isDone: true }),
} as OptimisticUpdateDefinition,
],
});
if (!task) {
return
);
};
export default TaskPage;
```
##### Advanced usage
The `useAction` hook currently only supports specifying optimistic updates. You can expect more features in future versions of Wasp.
Wasp's optimistic update API is deliberately small and focuses exclusively on updating Query caches (as that's the most common use case). You might need an API that offers more options or a higher level of control. If that's the case, instead of using Wasp's `useAction` hook, you can use *react-query*'s `useMutation` hook and directly work with [their low-level API](https://tanstack.com/query/v4/docs/framework/react/guides/optimistic-updates).
If you decide to use *react-query*'s API directly, you will need access to Query cache key. Wasp internally uses this key but abstracts it from the programmer. Still, you can easily obtain it by accessing the `queryCacheKey` property on any Query:
```ts
import { getTasks } from "wasp/client/operations"
const queryKey = getTasks.queryCacheKey
```
## Data Model / Automatic CRUD
If you have a lot of experience writing full-stack apps, you probably ended up doing some of the same things many times: listing data, adding data, editing it, and deleting it.
Wasp makes handling these boring bits easy by offering a higher-level concept called Automatic CRUD.
With a single spec, you can tell Wasp to automatically generate server-side logic (i.e., Queries and Actions) for creating, reading, updating and deleting [Entities](https://wasp.sh/docs/data-model/entities). As you update definitions for your Entities, Wasp automatically regenerates the backend logic.
:::caution[Early preview]
This feature is currently in early preview and we are actively working on it. Read more about [our plans](#future-of-crud-operations-in-wasp) for CRUD operations.
:::
### Overview
Imagine we have a `Task` entity and we want to enable CRUD operations for it:
```prisma title="schema.prisma"
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean
}
```
We can then define a new `crud` called `Tasks`.
We specify to use the `Task` entity and we enable the `getAll`, `get`, `create` and `update` operations (let's say we don't need the `delete` operation).
```ts title="main.wasp.ts"
import { app, crud } from "@wasp.sh/spec"
import { createTask } from "./src/tasks" with { type: "ref" }
export default app({
// ...
spec: [
crud("Tasks", "Task", {
getAll: {
isPublic: true, // by default only logged in users can perform operations
},
get: {},
create: {
overrideFn: createTask,
},
update: {},
}),
],
})
```
1. It uses default implementation for `getAll`, `get`, and `update`,
2. ... while specifying a custom implementation for `create`.
3. `getAll` will be public (no auth needed), while the rest of the operations will be private.
Here's what it looks like when visualized:

Visualization of the Tasks crud spec
We can now use the CRUD queries and actions we just specified in our client code.
Keep reading for an example of Automatic CRUD in action, or skip ahead for the [API Reference](#api-reference).
### Example: A Simple ToDo App
Let's create a full-app example that uses automatic CRUD. We'll stick to using the `Task` entity from the previous example, but we'll add a `User` entity and enable [username and password](https://wasp.sh/docs/auth/username-and-pass) based auth.

We are building a simple tasks app with username based auth
#### Creating the App
We can start by running `wasp new tasksCrudApp` and then adding the following to the `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/LoginPage" with { type: "ref" }
import { MainPage } from "./src/MainPage" with { type: "ref" }
import { SignupPage } from "./src/SignupPage" with { type: "ref" }
export default app({
name: "tasksCrudApp",
wasp: { version: "^0.25" },
title: "Tasks Crud App",
head: [""],
// We enabled auth and set the auth method to username and password
auth: {
userEntity: "User",
methods: {
usernameAndPassword: {},
},
onAuthFailedRedirectTo: "/login",
},
spec: [
// Tasks app routes
route("RootRoute", "/", page(MainPage, { authRequired: true, })),
route("LoginRoute", "/login", page(LoginPage)),
route("SignupRoute", "/signup", page(SignupPage)),
],
})
```
And let's define our entities in the `schema.prisma` file:
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
tasks Task[]
}
// We defined a Task entity on which we'll enable CRUD later on
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean
userId Int
user User @relation(fields: [userId], references: [id])
}
```
We can then run `wasp db migrate-dev` to create the database and run the migrations.
#### Adding CRUD to the `Task` Entity โจ
Let's add the following `crud` spec to our `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, crud } from "@wasp.sh/spec"
import { createTask } from "./src/tasks" with { type: "ref" }
export default app({
// ...
spec: [
crud("Tasks", "Task", {
getAll: {},
create: {
overrideFn: createTask,
},
}),
],
})
```
You'll notice that we enabled only `getAll` and `create` operations. This means that only these operations will be available.
We also overrode the `create` operation with a custom implementation. This means that the `create` operation will not be generated, but instead, the `createTask` function from `src/tasks.ts` will be used.
#### Our Custom `create` Operation
We need a custom `create` operation because we want to make sure that the task is connected to the user creating it. Automatic CRUD doesn't yet support this by default. Read more about the default implementations in the [`CrudOperations` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/CrudOperations).
Here's the `src/tasks.ts` file:
```ts title="src/tasks.ts"
import { type Tasks } from "wasp/server/crud"
import { type Task } from "wasp/entities"
import { HttpError } from "wasp/server"
type CreateTaskInput = { description: string; isDone: boolean }
export const createTask: Tasks.CreateAction = async (
args,
context
) => {
if (!context.user) {
throw new HttpError(401, "User not authenticated.")
}
const { description, isDone } = args
const { Task } = context.entities
return await Task.create({
data: {
description,
isDone,
// Connect the task to the user that is creating it
user: {
connect: {
id: context.user.id,
},
},
},
})
}
```
Wasp automatically generates the `Tasks.CreateAction` type based on the CRUD spec in your Wasp file. Use it to type the CRUD action's implementation.
The `Tasks.CreateAction` type works exactly like the types Wasp generates for [Queries](https://wasp.sh/docs/data-model/operations/queries#type-support-for-queries) and [Actions](https://wasp.sh/docs/data-model/operations/actions#type-support-for-actions). In other words, annotating the action with `Tasks.CreateAction` tells TypeScript about the type of the Action's `context` object, while the two type arguments allow you to specify the Action's inputs and outputs.
Read more about type support for CRUD overrides in the [API reference](#defining-the-overrides).
#### Using the Generated CRUD Operations on the Client
And let's use the generated operations in our client code:
```tsx title="src/MainPage.tsx"
import { Tasks } from "wasp/client/crud"
import { useState } from "react"
export const MainPage = () => {
// Thanks to full-stack type safety, all payload types are inferred
// automatically
const { data: tasks, isLoading, error } = Tasks.getAll.useQuery()
const createTask = Tasks.create.useAction()
const [taskDescription, setTaskDescription] = useState("")
function handleCreateTask() {
createTask({ description: taskDescription, isDone: false })
setTaskDescription("")
}
if (isLoading) return
Loading...
if (error) return
Error: {error.message}
return (
setTaskDescription(e.target.value)}
/>
{tasks.map((task) => (
{task.description}
))}
)
}
```
And here are the login and signup pages, where we are using Wasp's [Auth UI](https://wasp.sh/docs/auth/ui) components:
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
import { Link } from "react-router"
export function LoginPage() {
return (
Create an account
)
}
```
```tsx title="src/SignupPage.tsx"
import { SignupForm } from "wasp/client/auth"
export function SignupPage() {
return (
)
}
```
That's it. You can now run `wasp start` and see the app in action. โก๏ธ
You should see a login page and a signup page. After you log in, you should see a page with a list of tasks and a form to create new tasks.
### Future of CRUD Operations in Wasp
CRUD operations currently have a limited set of knowledge about the business logic they are implementing.
- For example, they don't know that a task should be connected to the user that is creating it. This is why we had to override the `create` operation in the example above.
- Another thing: they are not aware of the authorization rules. For example, they don't know that a user should not be able to create a task for another user. In the future, we will be adding role-based authorization to Wasp, and we plan to make CRUD operations aware of the authorization rules.
- Another issue is input validation and sanitization. For example, we might want to make sure that the task description is not empty.
CRUD operations are a mechanism for getting a backend up and running quickly, but it depends on the information it can get from the Wasp app. The more information that it can pick up from your app, the more powerful it will be out of the box.
We plan on supporting CRUD operations and growing them to become the easiest way to create your backend. Follow along on [this GitHub issue](https://github.com/wasp-lang/wasp/issues/1253) to see how we are doing.
### API Reference
#### Specifying CRUD Operations
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/crud)
#### [crud ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/crud)
[All the options for declaring CRUD operations in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/crud)
#### Defining the overrides
Like with actions and queries, you can define the implementation in a Javascript/Typescript file. The overrides are functions that take the following arguments:
- `args`
The arguments of the operation i.e. the data sent from the client.
- `context`
Context contains the `user` making the request and the `entities` object with the entity that's being operated on.
You can also import types for each of the functions you want to override by importing the `{crud name}` from `wasp/server/crud`. The available types are:
- `{crud name}.GetAllQuery`
- `{crud name}.GetQuery`
- `{crud name}.CreateAction`
- `{crud name}.UpdateAction`
- `{crud name}.DeleteAction`
If you have a CRUD named `Tasks`, you would import the types like this:
```ts
import { type Tasks } from "wasp/server/crud"
// Each of the types is a generic type, so you can use it like this:
export const getAllOverride: Tasks.GetAllQuery = async (
args,
context
) => {
// ...
}
```
For a usage example, check the [example guide](https://wasp.sh/docs/data-model/crud#adding-crud-to-the-task-entity-).
#### Using the CRUD operations in client code
On the client, you import the CRUD operations from `wasp/client/crud` by import the `{crud name}` object. For example, if you have a CRUD called `Tasks`, you would import the operations like this:
```tsx title="SomePage.tsx"
import { Tasks } from "wasp/client/crud"
```
You can then access the operations like this:
```tsx title="SomePage.tsx"
const { data } = Tasks.getAll.useQuery()
const { data } = Tasks.get.useQuery({ id: 1 })
const createAction = Tasks.create.useAction()
const updateAction = Tasks.update.useAction()
const deleteAction = Tasks.delete.useAction()
```
All CRUD operations are implemented with [Queries and Actions](https://wasp.sh/docs/data-model/operations/overview) under the hood, which means they come with all the features you'd expect (e.g., automatic SuperJSON serialization, full-stack type safety when using TypeScript)
## Data Model / Databases
[Entities](https://wasp.sh/docs/data-model/entities), [Operations](https://wasp.sh/docs/data-model/operations/overview) and [Automatic CRUD](https://wasp.sh/docs/data-model/crud) together make a high-level interface for working with your app's data. Still, all that data has to live somewhere, so let's see how Wasp deals with databases.
### Supported Database Backends
Wasp supports multiple database backends. We'll list and explain each one.
#### SQLite
The default database Wasp uses is [SQLite](https://www.sqlite.org/index.html).
When you create a new Wasp project, the `schema.prisma` file will have SQLite as the default database provider:
```prisma title="schema.prisma"
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
// ...
```
Read more about how Wasp uses the Prisma schema file in the [Prisma schema file](https://wasp.sh/docs/data-model/prisma-file) section.
When you use the SQLite database, Wasp sets the `DATABASE_URL` environment variable for you.
SQLite is a great way to get started with a new project because it doesn't require any configuration, but Wasp can only use it in development. Once you want to deploy your Wasp app to production, you'll need to switch to PostgreSQL and stick with it.
Fortunately, migrating from SQLite to PostgreSQL is pretty simple, and we have [a guide](#migrating-from-sqlite-to-postgresql) to help you.
#### PostgreSQL
[PostgreSQL](https://www.postgresql.org/) is the most advanced open-source database and one of the most popular databases overall. It's been in active development for 20+ years. Therefore, if you're looking for a battle-tested database, look no further.
To use PostgreSQL with Wasp, set the provider to `"postgresql"` in the `schema.prisma` file:
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// ...
```
Read more about how Wasp uses the Prisma schema file in the [Prisma schema file](https://wasp.sh/docs/data-model/prisma-file) section.
You'll have to ensure a database instance is running during development to use PostgreSQL. Wasp needs access to your database for commands such as `wasp start` or `wasp db migrate-dev`.
We cover all supported ways of connecting to a database in [the next section](#connecting-to-a-database).
### Connecting to a Database
#### SQLite
If you are using SQLite, you don't need to do anything special to connect to the database. Wasp will take care of it for you.
#### PostgreSQL
If you are using PostgreSQL, Wasp supports two ways of connecting to a database:
1. For managed experience, let Wasp spin up a ready-to-go development database for you.
2. For more control, you can specify a database URL and connect to an existing database that you provisioned yourself.
##### Using the Dev Database provided by Wasp
The command `wasp start db` will start a default PostgreSQL dev database for you.
Your Wasp app will automatically connect to it, just keep `wasp start db` running in the background. Also, make sure that:
- You have [Docker installed](https://www.docker.com/get-started/) and it's available in your `PATH`.
- The port `5432` isn't taken.
:::tip
In case you might want to connect to the dev database through the external tool like `psql` or [pgAdmin](https://www.pgadmin.org/), the credentials are printed in the console when you run `wasp db start`, at the very beginning.
:::
###### Customising the dev database {#custom-database}
The Wasp development database uses the [PostgreSQL 18 Docker image](https://hub.docker.com/_/postgres/tags?name=18) by default, and will set up its data volumes according to their guidance.
If you need to customise the development database, you can use the following options:
- `--db-image`: Specify a custom Docker image
Useful for PostgreSQL extensions or specific versions (for example, PostGIS, pgvector, etc.).
- `--db-volume-mount-path`: Specify the volume mount path inside the container
You only need to set this option if your custom `--db-image` is based on **PostgreSQL 17 or older** (check the `postgres:15` example below).
If the volume mount path is incorrect, the data won't be persisted in your development database.
Here are some examples of customising the development database:
```bash
# Use default PostgreSQL image:
wasp start db
# Same as:
wasp start db --db-image postgres:18
# Use PostgreSQL with PostGIS extension for geographic data:
wasp start db --db-image postgis/postgis:18-3.6
# Use PostgreSQL with pgvector extension for AI embeddings:
wasp start db --db-image pgvector/pgvector:pg18
# Use PostgreSQL version 15 (requires different volume path):
wasp start db --db-image postgres:15 --db-volume-mount-path /var/lib/postgresql/data
```
:::note
The custom Docker image you specify must use the `POSTGRES_DB`, `POSTGRES_USER`, and `POSTGRES_PASSWORD` environment variables when configuring the database. Wasp will use those values when connecting to the database. We recommend basing your image on the official [PostgreSQL Docker image](https://hub.docker.com/_/postgres), as it automatically uses these environment variables to set up the database name, user, and password.
:::
##### Connecting to an existing database
If you want to spin up your own dev database (or connect to an external one), you can tell Wasp about it using the `DATABASE_URL` environment variable. Wasp will use the value of `DATABASE_URL` as a connection string.
The easiest way to set the necessary `DATABASE_URL` environment variable is by adding it to the [.env.server](https://wasp.sh/docs/project/env-vars) file in the root dir of your Wasp project (if that file doesn't yet exist, create it):
```env title=".env.server"
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
```
Alternatively, you can set it inline when running `wasp` (this applies to all environment variables):
```bash
DATABASE_URL= wasp ...
```
This trick is useful for running a certain `wasp` command on a specific database. For example, you could do:
```bash
DATABASE_URL= wasp db seed myProductionSeed
```
This command seeds the data for a fresh staging or production database. Read more about [seeding the database](#seeding-the-database).
### Migrating from SQLite to PostgreSQL
To run your Wasp app in production, you'll need to switch from SQLite to PostgreSQL.
1. Set the provider to `"postgresql"` in the `schema.prisma` file:
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// ...
```
2. Delete all the old migrations, since they are SQLite migrations and can't be used with PostgreSQL, as well as the SQLite database by running [`wasp clean`](https://wasp.sh/docs/general/cli#project-commands):
```bash
rm -r migrations/
wasp clean
```
3. Ensure your new database is running (check the [section on connecting to a database](#connecting-to-a-database) to see how). Leave it running, since we need it for the next step.
4. In a different terminal, run `wasp db migrate-dev` to apply the changes and create a new initial migration.
5. That is it, you are all done!
### Seeding the Database
**Database seeding** is a term used for populating the database with some initial data.
Seeding is most commonly used for:
1. Getting the development database into a state convenient for working and testing.
2. Initializing any database (`dev`, `staging`, or `prod`) with essential data it requires to operate. For example, populating the Currency table with default currencies, or the Country table with all available countries.
#### Writing a Seed Function
You can define as many **seed functions** as you want in an array under the `db.seeds` field:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { devSeedSimple, prodSeed } from "./src/dbSeeds" with { type: "ref" }
export default app({
name: "MyApp",
// ...
db: {
seeds: [devSeedSimple, prodSeed],
},
})
```
Each seed function must be an async function that takes one argument, `prisma`, which is a [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client/crud) instance used to interact with the database. This is the same Prisma Client instance that Wasp uses internally.
Since a seed function falls under server-side code, it can import other server-side functions. This is convenient because you might want to seed the database using Actions.
Here's an example of a seed function that imports an Action:
```ts
import { createTask } from "./actions.js"
import type { DbSeedFn } from "wasp/server"
import { sanitizeAndSerializeProviderData } from "wasp/server/auth"
import type { AuthUser } from "wasp/auth"
import type { PrismaClient } from "wasp/server"
export const devSeedSimple: DbSeedFn = async (prisma) => {
const user = await createUser(prisma, {
username: "RiuTheDog",
password: "bark1234",
})
await createTask(
{ description: "Chase the cat", isDone: false },
{ user, entities: { Task: prisma.task } }
)
};
async function createUser(
prisma: PrismaClient,
data: { username: string, password: string }
): Promise {
const newUser = await prisma.user.create({
data: {
auth: {
create: {
identities: {
create: {
providerName: "username",
providerUserId: data.username,
providerData: await sanitizeAndSerializeProviderData<"username">({
hashedPassword: data.password
}),
},
},
},
},
},
})
return newUser
}
```
Wasp exports a type called `DbSeedFn` which you can use to easily type your seeding function. Wasp defines `DbSeedFn` like this:
```typescript
type DbSeedFn = (prisma: PrismaClient) => Promise
```
Annotating the function `devSeedSimple` with this type tells TypeScript:
- The seeding function's argument (`prisma`) is of type `PrismaClient`.
- The seeding function's return value is `Promise`.
#### Running seed functions
Run the command `wasp db seed` and Wasp will ask you which seed function you'd like to run (if you've defined more than one).
Alternatively, run the command `wasp db seed ` to choose a specific seed function right away, for example:
```
wasp db seed devSeedSimple
```
Check the [API Reference](#cli-commands-for-seeding-the-database) for more details on these commands.
:::tip
You'll often want to call `wasp db seed` right after you run `wasp db reset`, as it makes sense to fill the database with initial data after clearing it.
:::
### Customising the Prisma Client
Wasp interacts with the database using the [Prisma Client](https://www.prisma.io/docs/orm/prisma-client). To customize the client, define a function in the `db.prismaSetupFn` field that returns a Prisma Client instance. This allows you to configure features like [logging](https://www.prisma.io/docs/orm/prisma-client/observability-and-logging/logging) or [client extensions](https://www.prisma.io/docs/orm/prisma-client/client-extensions):
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { setUpPrisma } from "./src/prisma" with { type: "ref" }
export default app({
name: "MyApp",
// ...
db: {
prismaSetupFn: setUpPrisma,
},
})
```
```ts title="src/prisma.ts"
import { PrismaClient } from "@prisma/client"
export const setUpPrisma = () => {
const prisma = new PrismaClient({
log: ["query"],
}).$extends({
query: {
task: {
async findMany({ args, query }) {
args.where = {
...args.where,
description: { not: { contains: "hidden by setUpPrisma" } },
}
return query(args)
},
},
},
})
return prisma
}
```
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Db)
#### [Db ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Db)
[All the options for the db field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Db)
#### CLI Commands for Seeding the Database
Use one of the following commands to run the seed functions:
- `wasp db seed`
If you've only defined a single seed function, this command runs it. If you've defined multiple seed functions, it asks you to choose one interactively.
- `wasp db seed `
This command runs the seed function with the specified name. Wasp derives this name from the imported function name you list in `db.seeds`. For example, to run the seed function `devSeedSimple` which was defined like this:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { devSeedSimple } from "./src/dbSeeds" with { type: "ref" }
export default app({
name: "MyApp",
// ...
db: {
seeds: [
// ...
devSeedSimple,
],
},
})
```
Use the following command:
```
wasp db seed devSeedSimple
```
## Data Model / Prisma Schema File
Wasp uses [Prisma](https://www.prisma.io/) to interact with the database. Prisma is a "Next-generation Node.js and TypeScript ORM" that provides a type-safe API for working with your database.
With Prisma, you define your application's data model in a `schema.prisma` file. Read more about how Wasp Entities relate to Prisma models on the [Entities](https://wasp.sh/docs/data-model/entities) page.
In Wasp, the `schema.prisma` file is located in your project's root directory:
```c
.
โโโ main.wasp.ts
...
โโโ package.json
โโโ public
โโโ schema.prisma
โโโ src
โโโ tsconfig.json
โโโ tsconfig.src.json
โโโ tsconfig.wasp.json
โโโ vite.config.ts
```
Wasp uses the `schema.prisma` file to understand your app's data model and generate the necessary code to interact with the database.
### Wasp file and Prisma schema file
Let's see how Wasp and Prisma files work together to define your application.
Here's an example `schema.prisma` file where we defined some database options and two models (User and Task) with a one-to-many relationship:
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
tasks Task[]
}
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
user User @relation(fields: [userId], references: [id])
userId Int
}
```
Wasp reads this `schema.prisma` file and extracts the info about your database models and database config.
The `datasource` block defines which database you want to use (PostgreSQL in this case) and some other options.
The `generator` block defines how to generate the Prisma Client code that you can use in your application to interact with the database.

Relationship between Wasp file and Prisma file
Finally, Prisma models become Wasp Entities which can be then used in the `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { api, app, job, query } from "@wasp.sh/spec"
import { fooBar } from "./src/apis" with { type: "ref" }
import { getTasks } from "./src/queries" with { type: "ref" }
import { foo } from "./src/workers/bar" with { type: "ref" }
export default app({
// ...
spec: [
// Using Wasp Entities in the Wasp file.
query(getTasks, { entities: ["Task"] }),
job(foo, {
executor: "PgBoss",
entities: ["Task"],
}),
api("GET", "/foo/bar/:email", fooBar, {
entities: ["Task"],
}),
],
})
```
In the implementation of the `getTasks` query, `Task` is a Wasp Entity that corresponds to the `Task` model defined in the `schema.prisma` file.
The same goes for the `myJob` job and `fooBar` API, where `Task` is used as an Entity.
To learn more about the relationship between Wasp Entities and Prisma models, check out the [Entities](https://wasp.sh/docs/data-model/entities) page.
### Wasp-specific Prisma configuration
Wasp mostly lets you use the Prisma schema file as you would in any other JS/TS project. However, there are some Wasp-specific rules you need to follow.
#### The `datasource` block
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```
Wasp takes the `datasource` you write and use it as-is.
There are some rules you need to follow:
- You can only use `"postgresql"` or `"sqlite"` as the `provider` because Wasp only supports PostgreSQL and SQLite databases for now.
- You must set the `url` field to `env("DATABASE_URL")` so that Wasp can work properly with your database.
#### The `generator` blocks
```prisma title="schema.prisma"
generator client {
provider = "prisma-client-js"
}
```
Wasp requires that there is a `generator` block with `provider = "prisma-client-js"` in the `schema.prisma` file.
You can add additional generators if you need them in your project.
#### The `model` blocks
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
tasks Task[]
}
model Task {
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
user User @relation(fields: [userId], references: [id])
userId Int
}
```
You can define your models in any way you like, if it's valid Prisma schema code, it will work with Wasp.
#### The `enum` blocks
As our applications grow in complexity, we might want to use [Prisma `enum`s](https://www.prisma.io/docs/orm/prisma-schema/data-model/models#defining-enums) to closely define our app's domain. For example, if we had a `Task` model with a boolean `isDone`, and we wanted to start to track whether it is in progress, we could migrate the field to a more expressive type:
```prisma title="schema.prisma"
enum TaskStatus {
NotStarted
Doing
Done
}
model Task {
...
state TaskStatus @default(NotStarted)
}
```
Make sure to check [Prisma's enum compatibility with your database](https://www.prisma.io/docs/orm/reference/database-features#misc). If it works with Prisma, it will work with Wasp.
##### How to use `enum`s in your code
If you need to access your `enum` cases and their values from your server, you can import them directly from `@prisma/client`:
```ts title="src/queries.ts"
import { TaskState } from "@prisma/client";
import { Task } from "wasp/entities";
import { type GetTasks } from "wasp/server/operations";
export const getTasks: GetTasks = async (args, context) => {
return context.entities.Task.findMany({
orderBy: { id: "asc" },
where: { NOT: { state: TaskState.Done } },
});
};
```
You can also access them from your client code:
```ts title="src/views/TaskList.tsx"
import { TaskState } from "@prisma/client";
import { Task } from "wasp/entities";
const TaskRow = ({ task }: { task: Task }) => {
return (
{task.description}
);
};
```
:::note[Triple slash comments]
Wasp only supports `///` in the *leading* position:
```prisma title="schema.prisma"
model User {
/// The unique identifier for the user.
id Int @id @default(autoincrement())
}
```
However, Wasp does not support `///` comments in the *trailing* position:
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement()) /// This is not supported
}
```
We are aware of this issue and are tracking it at [#3041](https://github.com/wasp-lang/wasp/issues/3041), let us know if this is something you need.
:::
### Prisma preview features
Prisma is still in active development and some of its features are not yet stable. To enable various preview features in Prisma, you need to add the `previewFeatures` field to the `generator` block in the `schema.prisma` file.
For example, one useful Prisma preview feature is PostgreSQL extensions support, which allows you to use PostgreSQL extensions like `pg_vector` or `pg_trgm` in your database schema:
```prisma title="schema.prisma"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [pgvector(map: "vector")]
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["postgresqlExtensions"]
}
// ...
```
Read more about preview features in the Prisma docs [here](https://www.prisma.io/docs/orm/reference/preview-features/client-preview-features) or about using PostgreSQL extensions [here](https://www.prisma.io/docs/orm/prisma-schema/postgresql-extensions).
## Authentication / Overview
Auth is an essential piece of any serious application. That's why Wasp provides authentication and authorization support out of the box.
Enabling auth for your app is optional and can be done by configuring the `auth` field of your `app` spec:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "MyApp",
wasp: { version: "^0.25" },
title: "My app",
head: [""],
auth: {
userEntity: "User",
methods: {
usernameAndPassword: {}, // use this or email, not both
email: {}, // use this or usernameAndPassword, not both
google: {},
gitHub: {},
},
onAuthFailedRedirectTo: "/someRoute",
},
// ...
})
```
Read more about the `auth` field options in the [API Reference](#api-reference) section.
We will provide a quick overview of auth in Wasp and link to more detailed documentation for each auth method.
### Available auth methods
Wasp supports the following auth methods:
#### [Email ยป](https://wasp.sh/docs/auth/email)
[Email verification, password reset, etc.](https://wasp.sh/docs/auth/email)
#### [Username & Password ยป](https://wasp.sh/docs/auth/username-and-pass)
[The simplest way to get started](https://wasp.sh/docs/auth/username-and-pass)
#### [Google ยป](https://wasp.sh/docs/auth/social-auth/google)
[Users sign in with their Google account](https://wasp.sh/docs/auth/social-auth/google)
#### [Github ยป](https://wasp.sh/docs/auth/social-auth/github)
[Users sign in with their Github account](https://wasp.sh/docs/auth/social-auth/github)
#### [Keycloak ยป](https://wasp.sh/docs/auth/social-auth/keycloak)
[Users sign in with their Keycloak account](https://wasp.sh/docs/auth/social-auth/keycloak)
#### [Slack ยป](https://wasp.sh/docs/auth/social-auth/slack)
[Users sign in with their Slack account](https://wasp.sh/docs/auth/social-auth/slack)
#### [Discord ยป](https://wasp.sh/docs/auth/social-auth/discord)
[Users sign in with their Discord account](https://wasp.sh/docs/auth/social-auth/discord)
Click on each auth method for more details.
Let's say we enabled the [Username & password](https://wasp.sh/docs/auth/username-and-pass) authentication.
We get an auth backend with signup and login endpoints. We also get the `user` object in our [Operations](https://wasp.sh/docs/data-model/operations/overview) and we can decide what to do based on whether the user is logged in or not.
We would also get the [Auth UI](https://wasp.sh/docs/auth/ui) generated for us. We can set up our login and signup pages where our users can **create their account** and **login**. We can then protect certain pages by setting `authRequired: true` for them. This will make sure that only logged-in users can access them.
We will also have access to the `user` object in our frontend code, so we can show different UI to logged-in and logged-out users. For example, we can show the user's name in the header alongside a **logout button** or a login button if the user is not logged in.
### Different ways to use auth
When you have decided which auth methods you want to support, you can also choose how you want to present the authorization flows to your users.
##### Generated components
This is the fastest way to ship, with Wasp generating ready-made components for your app. They allow for some customization to make them consistent with your app. You don't need to implement any UI or logic, and they just work.
#### [Email ยป](https://wasp.sh/docs/auth/email)
#### [Username and password ยป](https://wasp.sh/docs/auth/username-and-pass)
#### [Social Auth ยป](https://wasp.sh/docs/auth/social-auth/overview)
##### Make your own UI {#custom-auth-ui}
Wasp is flexible enough to let you completely customize your login and signup interface. We give you the auth related functions, and you decide how and when to call them. This allows for total customization of the look-and-feel, and the interaction, but it needs a bit more work.
#### [Email ยป](https://wasp.sh/docs/auth/email/create-your-own-ui)
#### [Username and password ยป](https://wasp.sh/docs/auth/username-and-pass/create-your-own-ui)
#### [Social Auth ยป](https://wasp.sh/docs/auth/social-auth/create-your-own-ui)
:::tip
You don't have to choose one *or* the other! Mix-and-match, and use what you need in each moment. For example, you can create a custom signup screen, but use Wasp's generated components for login.
:::
##### Custom login and signup actions
The previously discussed options should cover the vast majority of cases. But, for the few instances where it is not enough, you can [create your own signup flows](https://wasp.sh/docs/auth/advanced/custom-auth-actions), with completely custom logic. This is not recommended, and reserved for advanced use cases. Please check first if other Wasp features (mainly [auth hooks](https://wasp.sh/docs/auth/auth-hooks)) can handle your requirements.
### Protecting a page with `authRequired`
When declaring a page, you can set the `authRequired` property.
If you set it to `true`, only authenticated users can access the page. Unauthenticated users are redirected to a route defined by the `auth.onAuthFailedRedirectTo` field.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import Main from "./src/pages/Main" with { type: "ref" }
export default app({
// ...
spec: [
route("MainRoute", "/", page(Main, { authRequired: true })),
],
})
```
:::caution[Requires auth method]
You can only use `authRequired` if your app uses one of the [available auth methods](#available-auth-methods).
:::
If `authRequired` is set to `true`, the page's React component (passed as the first argument to `page(...)`) receives the `user` object as a prop. Read more about the `user` object in the [Accessing the logged-in user section](#accessing-the-logged-in-user).
### Logout action
We provide an action for logging out the user. Here's how you can use it:
```tsx title="src/components/LogoutButton.tsx"
import { logout } from "wasp/client/auth"
const LogoutButton = () => {
return
}
```
### Accessing the logged-in user
You can get access to the `user` object both on the server and on the client. The `user` object contains the logged-in user's data.
The `user` object has all the fields that you defined in your `User` entity. In addition to that, it will also contain all the auth-related fields that Wasp stores. This includes things like the `username` or the email verification status. For example, if you have a user that signed up using an email and password, the `user` object might look like this:
```ts
const user = {
// User data
id: "cluqsex9500017cn7i2hwsg17",
address: "Some address",
// Auth methods specific data
identities: {
email: {
id: "user@app.com",
isEmailVerified: true,
emailVerificationSentAt: "2024-04-08T10:06:02.204Z",
passwordResetSentAt: null,
},
},
}
```
You can read more about how the `User` is connected to the rest of the auth system and how you can access the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities) section of the docs.
#### On the client
There are two ways to access the `user` object on the client:
- the `user` prop
- the `useAuth` hook
##### Getting the `user` in authenticated routes
If the page's spec sets `authRequired` to `true`, the page's React component receives the `user` object as a prop. This is the simplest way to access the user inside an authenticated page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import Account from "./src/pages/Account" with { type: "ref" }
export default app({
// ...
spec: [
route("AccountRoute", "/account", page(Account, {
authRequired: true
})),
],
})
```
```tsx title="src/pages/Account.tsx"
import type { AuthUser } from "wasp/auth";
import Button from "./Button";
import { logout } from "wasp/client/auth";
const AccountPage = ({ user }: { user: AuthUser }) => {
return (
{JSON.stringify(user, null, 2)}
);
};
export default AccountPage;
```
##### Getting the `user` in non-authenticated routes
Wasp provides a React hook you can use in the client components - `useAuth`.
This hook is a thin wrapper over Wasp's `useQuery` hook and returns data in the same format.
```tsx title="src/pages/MainPage.tsx"
import { useAuth, logout } from "wasp/client/auth";
import { Link } from "react-router";
import Todo from "../Todo";
export function Main() {
const { data: user } = useAuth();
if (!user) {
return (
Please login or{" "}
sign up.
);
} else {
return (
<>
>
);
}
}
```
#### On the server
##### Using the `context.user` object
When authentication is enabled, all [queries and actions](https://wasp.sh/docs/data-model/operations/overview) have access to the `user` object through the `context` argument. `context.user` contains all User entity's fields and the auth identities connected to the user. We strip out the `hashedPassword` field from the identities for security reasons.
```ts title="src/actions.ts"
import type { Task } from "wasp/entities";
import type { CreateTask } from "wasp/server/operations";
import { HttpError } from "wasp/server";
type CreateTaskPayload = Pick;
export const createTask: CreateTask = async (
args,
context,
) => {
if (!context.user) {
throw new HttpError(403);
}
const Task = context.entities.Task;
return Task.create({
data: {
description: args.description,
user: {
connect: { id: context.user.id },
},
},
});
};
```
To implement access control in your app, each operation must check `context.user` and decide what to do. For example, if `context.user` is `undefined` inside a private operation, the user's access should be denied.
When using WebSockets, the `user` object is also available on the `socket.data` object. Read more in the [WebSockets section](https://wasp.sh/docs/advanced/web-sockets#websocketfn).
### Sessions
Wasp's auth uses sessions to keep track of the logged-in user. The session is stored in `localStorage` on the client and in the database on the server. Under the hood, Wasp uses the excellent [Lucia Auth v3](https://v3.lucia-auth.com/) library for session management.
When users log in, Wasp creates a session for them and stores it in the database. The session is then sent to the client and stored in `localStorage`. When users log out, Wasp deletes the session from the database and from `localStorage`.
### User Entity
#### Password Hashing
If you are saving a user's password in the database, you should **never** save it as plain text. You can use Wasp's helper functions for serializing and deserializing provider data which will automatically hash the password for you:
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { updatePassword } from "./src/auth" with { type: "ref" }
export default app({
// ...
spec: [
action(updatePassword),
],
})
```
```ts title="src/auth.ts"
import {
createProviderId,
findAuthIdentity,
updateAuthIdentityProviderData,
getProviderDataWithPassword,
} from "wasp/server/auth";
import type { UpdatePassword } from "wasp/server/operations";
export const updatePassword: UpdatePassword<
{ email: string; password: string },
void
> = async (args, context) => {
const providerId = createProviderId("email", args.email);
const authIdentity = await findAuthIdentity(providerId);
if (!authIdentity) {
throw new HttpError(400, "Unknown user");
}
const providerData = getProviderDataWithPassword<"email">(
authIdentity.providerData,
);
// Updates the password and hashes it automatically.
await updateAuthIdentityProviderData(providerId, providerData, {
hashedPassword: args.password,
});
};
```
#### Default Validations
When you are using the default authentication flow, Wasp validates the fields with some default validations. These validations run if you use Wasp's built-in [Auth UI](https://wasp.sh/docs/auth/ui) or if you use the provided auth actions.
If you decide to create your [custom auth actions](https://wasp.sh/docs/auth/advanced/custom-auth-actions), you'll need to run the validations yourself.
Default validations depend on the auth method you use.
##### Username & Password
If you use [Username & password](https://wasp.sh/docs/auth/username-and-pass) authentication, the default validations are:
- The `username` must not be empty
- The `password` must not be empty, have at least 8 characters, and contain a number
Note that `username`s are stored in a **case-insensitive** manner.
##### Email
If you use [Email](https://wasp.sh/docs/auth/email) authentication, the default validations are:
- The `email` must not be empty and a valid email address
- The `password` must not be empty, have at least 8 characters, and contain a number
Note that `email`s are stored in a **case-insensitive** manner.
### Customizing the Signup Process
Sometimes you want to include **extra fields** in your signup process, like first name and last name and save them in the `User` entity.
For this to happen:
- you need to define the fields that you want saved in the database,
- you need to customize the `SignupForm` (in the case of [Email](https://wasp.sh/docs/auth/email) or [Username & Password](https://wasp.sh/docs/auth/username-and-pass) auth)
Other times, you might need to just add some **extra UI** elements to the form, like a checkbox for terms of service. In this case, customizing only the UI components is enough.
Let's see how to do both.
#### 1. Defining Extra Fields
If we want to **save** some extra fields in our signup process, we need to tell our app they exist.
We do that by defining an object where the keys represent the field name, and the values are functions that receive the data sent from the client\* and return the value of the field.
\* We exclude the `password` field from this object to prevent it from being saved as plain-text in the database. The `password` field is handled by Wasp's auth backend.
First, we add the `auth.methods.{authMethod}.userSignupFields` field in our `main.wasp.ts` file. The `{authMethod}` depends on the auth method you are using.
For example, if you are using [Username & Password](https://wasp.sh/docs/auth/username-and-pass), you would add the `auth.methods.usernameAndPassword.userSignupFields` field:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { userSignupFields } from "./src/auth/signup" with { type: "ref" }
export default app({
name: "myApp",
// ...
auth: {
userEntity: "User",
methods: {
usernameAndPassword: {
userSignupFields,
},
},
onAuthFailedRedirectTo: "/login",
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
address String?
}
```
Then we'll define the `userSignupFields` object in the `src/auth/signup.ts` file:
```ts title="src/auth/signup.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
address: async (data) => {
const address = data.address;
if (typeof address !== "string") {
throw new Error("Address is required");
}
if (address.length < 5) {
throw new Error("Address must be at least 5 characters long");
}
return address;
},
});
```
Read more about the `userSignupFields` object in the [API Reference](#signup-fields-customization).
Keep in mind, that these field names need to exist on the `userEntity` you defined in your `main.wasp.ts` file e.g. `address` needs to be a field on the `User` entity you defined in the `schema.prisma` file.
The field function will receive the data sent from the client and it needs to return the value that will be saved into the database. If the field is invalid, the function should throw an error.
:::info[Using Validation Libraries]
You can use any validation library you want to validate the fields. For example, you can use `zod` like this:
Click to see the code
```ts title="src/auth/signup.ts"
import { defineUserSignupFields } from "wasp/server/auth";
import * as z from "zod";
export const userSignupFields = defineUserSignupFields({
address: (data) => {
const AddressSchema = z
.string({
required_error: "Address is required",
invalid_type_error: "Address must be a string",
})
.min(10, "Address must be at least 10 characters long");
const result = AddressSchema.safeParse(data.address);
if (result.success === false) {
throw new Error(result.error.issues[0].message);
}
return result.data;
},
});
```
:::
Now that we defined the fields, Wasp knows how to:
1. Validate the data sent from the client
2. Save the data to the database
Next, let's see how to customize [Auth UI](https://wasp.sh/docs/auth/ui) to include those fields.
#### 2. Customizing the Signup Component
:::tip[Using Custom Signup Component]
If you are not using Wasp's Auth UI, you can skip this section. Just make sure to include the extra fields in your custom signup form.
Read more about using the signup actions for:
- [Email auth](https://wasp.sh/docs/auth/email/create-your-own-ui)
- [Username & password auth](https://wasp.sh/docs/auth/username-and-pass/create-your-own-ui)
:::
If you are using Wasp's Auth UI, you can customize the `SignupForm` component by passing the `additionalFields` prop to it. It can be either a list of extra fields or a render function.
##### Using a List of Extra Fields
When you pass in a list of extra fields to the `SignupForm`, they are added to the form one by one, in the order you pass them in.
Inside the list, there can be either **objects** or **render functions** (you can combine them):
1. Objects are a simple way to describe new fields you need, but a bit less flexible than render functions.
2. Render functions can be used to render any UI you want, but they require a bit more code. The render functions receive the `react-hook-form` object and the form state object as arguments.
```tsx title="src/SignupPage.tsx"
import {
SignupForm,
FormError,
FormInput,
FormItemGroup,
FormLabel,
} from "wasp/client/auth";
export const SignupPage = () => {
return (
{
return (
Phone Number
{form.formState.errors.phoneNumber && (
{form.formState.errors.phoneNumber.message}
)}
);
},
]}
/>
);
};
```
Read more about the extra fields in the [API Reference](#signupform-customization).
##### Using a Single Render Function
Instead of passing in a list of extra fields, you can pass in a render function which will receive the `react-hook-form` object and the form state object as arguments. What ever the render function returns, will be rendered below the default fields.
```tsx title="src/SignupPage.tsx"
import { SignupForm, FormItemGroup } from "wasp/client/auth";
export const SignupPage = () => {
return (
{
const username = form.watch("username");
return (
username && (
Hello there {username} ๐
)
);
}}
/>
);
};
```
Read more about the render function in the [API Reference](#signupform-customization).
### API Reference
#### Auth Fields
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
#### [Auth ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
[All the options for the auth field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
#### Signup Fields Customization
If you want to add extra fields to the signup process, the server needs to know how to save them to the database. You do that by defining the `auth.methods.{authMethod}.userSignupFields` field in your `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { userSignupFields } from "./src/auth/signup" with { type: "ref" }
export default app({
name: "myApp",
// ...
auth: {
userEntity: "User",
methods: {
usernameAndPassword: {
userSignupFields,
},
},
onAuthFailedRedirectTo: "/login",
},
// ...
})
```
Then we'll export the `userSignupFields` object from the `src/auth/signup.ts` file:
```ts title="src/auth/signup.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
address: async (data) => {
const address = data.address;
if (typeof address !== "string") {
throw new Error("Address is required");
}
if (address.length < 5) {
throw new Error("Address must be at least 5 characters long");
}
return address;
},
});
```
The `userSignupFields` object is an object where the keys represent the field name, and the values are functions that receive the data sent from the client\* and return the value of the field.
If the value that the function received is invalid, the function should throw an error.
\* We exclude the `password` field from this object to prevent it from being saved as plain text in the database. The `password` field is handled by Wasp's auth backend.
#### `SignupForm` Customization
To customize the `SignupForm` component, you need to pass in the `additionalFields` prop. It can be either a list of extra fields or a render function.
```tsx title="src/SignupPage.tsx"
import {
SignupForm,
FormError,
FormInput,
FormItemGroup,
FormLabel,
} from "wasp/client/auth";
export const SignupPage = () => {
return (
{
return (
Phone Number
{form.formState.errors.phoneNumber && (
{form.formState.errors.phoneNumber.message}
)}
);
},
]}
/>
);
};
```
The extra fields can be either **objects** or **render functions** (you can combine them):
1. Objects are a simple way to describe new fields you need, but a bit less flexible than render functions.
The objects have the following properties:
- `name` required
- the name of the field
- `label` required
- the label of the field (used in the UI)
- `type` required
- the type of the field, which can be `input` or `textarea`
- `validations`
- an object with the validation rules for the field. The keys are the validation names, and the values are the validation error messages. Read more about the available validation rules in the [react-hook-form docs](https://react-hook-form.com/api/useform/register#register).
2. Render functions receive the `react-hook-form` object and the form state as arguments, and they can use them to render arbitrary UI elements.
The render function has the following signature:
```ts
type AdditionalSignupFieldRenderFn = (
hookForm: UseFormReturn,
formState: FormState
) => React.ReactNode
```
- `form` required
The `react-hook-form` object, read more about it in the [react-hook-form docs](https://react-hook-form.com/api/useform). You need to use the `form.register` function to register your fields
- `state` required
The form state object, which has the following properties:
- `isLoading: boolean`
Whether the form is currently submitting
## Authentication / Auth UI
To make using authentication in your app as easy as possible, Wasp generates the server-side code but also the client-side UI for you. It enables you to quickly get the login, signup, password reset and email verification flows in your app.
Below we cover all of the available UI components and how to use them.

:::note
Remember that if you need a more custom approach, you can always [create your own UI](https://wasp.sh/docs/auth/overview#custom-auth-ui).
:::
### Overview
After Wasp generates the UI components for your auth, you can use it as is, or customize it to your liking.
Based on the authentication providers you enabled in your `main.wasp.ts` file, the Auth UI will show the corresponding UI (form and buttons). For example, if you enabled e-mail authentication:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "MyApp",
//...
auth: {
methods: {
email: {},
},
// ...
},
// ...
})
```
You'll get the following UI:

And then if you enable Google and Github:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "MyApp",
//...
auth: {
methods: {
email: {},
google: {},
gitHub: {},
},
// ...
},
// ...
})
```
The form will automatically update to look like this:

Let's go through all of the available components and how to use them.
### Auth Components
The following components are available for you to use in your app:
- [Login form](#login-form)
- [Signup form](#signup-form)
- [Forgot password form](#forgot-password-form)
- [Reset password form](#reset-password-form)
- [Verify email form](#verify-email-form)
#### Login Form
Used with [Username & Password](https://wasp.sh/docs/auth/username-and-pass), [Email](https://wasp.sh/docs/auth/email), [Github](https://wasp.sh/docs/auth/social-auth/github), [Google](https://wasp.sh/docs/auth/social-auth/google), [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak), [Slack](https://wasp.sh/docs/auth/social-auth/slack) and [Discord](https://wasp.sh/docs/auth/social-auth/discord) authentication.

You can use the `LoginForm` component to build your login page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/LoginPage" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
// Use it like this
export function LoginPage() {
return
}
```
It will automatically show the correct authentication providers based on your `main.wasp.ts` file.
#### Signup Form
Used with [Username & Password](https://wasp.sh/docs/auth/username-and-pass), [Email](https://wasp.sh/docs/auth/email), [Github](https://wasp.sh/docs/auth/social-auth/github), [Google](https://wasp.sh/docs/auth/social-auth/google), [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak), [Slack](https://wasp.sh/docs/auth/social-auth/slack) and [Discord](https://wasp.sh/docs/auth/social-auth/discord) authentication.

You can use the `SignupForm` component to build your signup page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { SignupPage } from "./src/SignupPage" with { type: "ref" }
export default app({
// ...
spec: [
route("SignupRoute", "/signup", page(SignupPage)),
],
})
```
```tsx title="src/SignupPage.tsx"
import { SignupForm } from "wasp/client/auth"
// Use it like this
export function SignupPage() {
return
}
```
It will automatically show the correct authentication providers based on your `main.wasp.ts` file.
Read more about customizing the signup process like adding additional fields or extra UI in the [Auth Overview](https://wasp.sh/docs/auth/overview#customizing-the-signup-process) section.
#### Forgot Password Form
Used with [Email](https://wasp.sh/docs/auth/email) authentication.
If users forget their password, they can use this form to reset it.
*(Inlined image: Forgot password form)*
You can use the `ForgotPasswordForm` component to build your own forgot password page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { ForgotPasswordPage } from "./src/ForgotPasswordPage" with { type: "ref" }
export default app({
// ...
spec: [
route(
"RequestPasswordResetRoute",
"/request-password-reset",
page(ForgotPasswordPage)
),
],
})
```
```tsx title="src/ForgotPasswordPage.tsx"
import { ForgotPasswordForm } from "wasp/client/auth"
// Use it like this
export function ForgotPasswordPage() {
return
}
```
#### Reset Password Form
Used with [Email](https://wasp.sh/docs/auth/email) authentication.
After users click on the link in the email they receive after submitting the forgot password form, they will be redirected to this form where they can reset their password.

You can use the `ResetPasswordForm` component to build your reset password page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { ResetPasswordPage } from "./src/ResetPasswordPage" with { type: "ref" }
export default app({
// ...
spec: [
route("PasswordResetRoute", "/password-reset", page(ResetPasswordPage)),
],
})
```
```tsx title="src/ResetPasswordPage.tsx"
import { ResetPasswordForm } from "wasp/client/auth"
// Use it like this
export function ResetPasswordPage() {
return
}
```
#### Verify Email Form
Used with [Email](https://wasp.sh/docs/auth/email) authentication.
After users sign up, they will receive an email with a link to this form where they can verify their email.

You can use the `VerifyEmailForm` component to build your email verification page:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { VerifyEmailPage } from "./src/VerifyEmailPage" with { type: "ref" }
export default app({
// ...
spec: [
route("EmailVerificationRoute", "/email-verification", page(VerifyEmailPage)),
],
})
```
```tsx title="src/VerifyEmailPage.tsx"
import { VerifyEmailForm } from "wasp/client/auth"
// Use it like this
export function VerifyEmailPage() {
return
}
```
### Customization ๐ ๐ป
You customize all of the available forms by passing props to them.
Props you can pass to all of the forms:
1. `appearance` - customize the form colors (via design tokens)
2. `logo` - path to your logo
3. `socialLayout` - layout of the social buttons, which can be `vertical` or `horizontal`
#### 1. Customizing the Colors
We use CSS variables in our styling so you can customize the styles by overriding the default theme tokens.
:::info[List of all available tokens]
See the [list of all available tokens](https://github.com/wasp-lang/wasp/blob/release/waspc/data/Generator/templates/sdk/wasp/auth/forms/types.ts) which you can override.
:::
```ts title="src/appearance.ts"
import type { CustomizationOptions } from "wasp/client/auth"
export const authAppearance: CustomizationOptions["appearance"] = {
colors: {
brand: "#5969b8", // blue
brandAccent: "#de5998", // pink
submitButtonText: "white",
},
}
```
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
import { authAppearance } from "./appearance"
export function LoginPage() {
return (
)
}
```
We recommend defining your appearance in a separate file and importing it into your components.
#### 2. Using Your Logo
You can add your logo to the Auth UI by passing the `logo` prop to any of the components.
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
import Logo from "./logo.png"
export function LoginPage() {
return (
)
}
```
#### 3. Social Buttons Layout
You can change the layout of the social buttons by passing the `socialLayout` prop to any of the components. It can be either `vertical` or `horizontal` (default).
If we pass in `vertical`:
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
export function LoginPage() {
return (
)
}
```
We get this:

#### Let's Put Everything Together ๐ช
If we provide the logo and custom colors:
```ts title="src/appearance.ts"
import type { CustomizationOptions } from "wasp/client/auth"
export const appearance: CustomizationOptions["appearance"] = {
colors: {
brand: "#5969b8", // blue
brandAccent: "#de5998", // pink
submitButtonText: "white",
},
}
```
```tsx title="src/LoginPage.tsx"
import { LoginForm } from "wasp/client/auth"
import { authAppearance } from "./appearance"
import todoLogo from "./todoLogo.png"
export function LoginPage() {
return
}
```
We get a form looking like this:

## Authentication / Username & Password / Overview
Wasp supports username & password authentication out of the box with login and signup flows. It provides you with the server-side implementation and the UI components for the client side.
### Setting Up Username & Password Authentication
To set up username authentication we need to:
1. Enable username authentication in the Wasp file
2. Add the `User` entity
3. Add the auth routes and pages
4. Use Auth UI components in our pages
Structure of the `main.wasp.ts` file we will end up with:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { SignupPage } from "./src/pages/auth" with { type: "ref" }
// Configuring e-mail authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("SignupRoute", "/signup", page(SignupPage)),
// ...
],
})
```
#### 1. Enable Username Authentication
Let's start with adding the following to our `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the user entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable username authentication
usernameAndPassword: {},
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
Read more about the `usernameAndPassword` auth method options in the [`UsernameAndPasswordConfig` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/UsernameAndPasswordConfig).
#### 2. Add the User Entity
The `User` entity can be as simple as including only the `id` field:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
You can read more about how the `User` is connected to the rest of the auth system and how you can access the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities) section of the docs.
#### 3. Add the Routes and Pages
Next, we need to define the routes and pages for the authentication pages.
Add the following to the `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage, SignupPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
route("SignupRoute", "/signup", page(SignupPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 4. Create the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import { LoginForm, SignupForm } from "wasp/client/auth"
import { Link } from "react-router"
export function LoginPage() {
return (
Don't have an account yet? go to signup.
)
}
export function SignupPage() {
return (
I already have an account (go to login).
)
}
// A layout component to center the content
export function Layout({ children }: { children: React.ReactNode }) {
return (
{children}
)
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
That's it! We have set up username authentication in our app. ๐
Running `wasp db migrate-dev` and then `wasp start` should give you a working app with username authentication. If you want to put some of the pages behind authentication, read the [auth overview docs](https://wasp.sh/docs/auth/overview).
:::caution[Using multiple auth identities for a single user]
Wasp currently doesn't support multiple auth identities for a single user. This means, for example, that a user can't have both an email-based auth identity and a Google-based auth identity. This is something we will add in the future with the introduction of the [account merging feature](https://github.com/wasp-lang/wasp/issues/954).
Account merging means that multiple auth identities can be merged into a single user account. For example, a user's email and Google identity can be merged into a single user account. Then the user can log in with either their email or Google account and they will be logged into the same account.
:::
### Using Auth
To read more about how to set up the logout button and how to get access to the logged-in user in our client and server code, read the [auth overview docs](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's username like this:
```ts
const usernameIdentity = user.identities.username
// Username that the user used to sign up, e.g. "fluffyllama"
usernameIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
#### [Auth ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
[All the options for the auth field of the app spec, including userEntity.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/UsernameAndPasswordConfig)
#### [UsernameAndPasswordConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/UsernameAndPasswordConfig)
[All the options for the usernameAndPassword auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/UsernameAndPasswordConfig)
Read more about the `userSignupFields` function in the [Auth Overview docs](https://wasp.sh/docs/auth/overview#signup-fields-customization).
## Authentication / Username & Password / Create your own UI
The login and signup flows are pretty standard: they allow the user to sign up and then log in with their username and password. The signup flow validates the username and password and then creates a new user entity in the database.
:::tip
Read more about the default email and password validation rules in the [auth overview docs](https://wasp.sh/docs/auth/overview#default-validations).
:::
Even though Wasp offers premade [Auth UI](https://wasp.sh/docs/auth/ui) for your authentication flows, there are times where you might want more customization, so we also give you the option to create your own UI and call Wasp's auth actions on your own code, similar to how Auth UI does it under the hood.
### Example code
Below you can find a starting point for making your own UI in the client code. You can customize any of its look and behaviour, just make sure to call the `signup()` or `login()` functions imported from `wasp/client/auth`.
#### Sign-up
```tsx title="src/pages/auth.tsx"
import { login, signup } from 'wasp/client/auth'
import { useState } from 'react'
import { useNavigate } from 'react-router'
export function SignupPage() {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const navigate = useNavigate()
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
await signup({ username, password })
await login({ username, password })
navigate('/')
} catch (error: unknown) {
setError(error as Error)
}
}
return (
)
}
```
#### Login
```tsx title="src/pages/auth.tsx"
import { login } from 'wasp/client/auth'
import { useState } from 'react'
import { useNavigate } from 'react-router'
export function LoginPage() {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const navigate = useNavigate()
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
await login({ username, password })
navigate('/')
} catch (error: unknown) {
setError(error as Error)
}
}
return (
)
}
```
### API Reference
You can import the following functions from `wasp/client/auth`:
#### `login()`
An action for logging in the user.
It takes one argument:
- `data: object` required
It has the following fields:
- `username: string` required
- `password: string` required
:::note
When using the exposed `login()` function, make sure to implement your redirect on success login logic (e.g. redirecting to home).
:::
#### `signup()`
An action for signing up the user. This action does not log in the user, you still need to call `login()`.
It takes one argument:
- `data: object` required
It has the following fields:
- `username: string` required
- `password: string` required
:::info
By default, Wasp will only save the `username` and `password` fields. If you want to add extra fields to your signup process, read about [defining extra signup fields](https://wasp.sh/docs/auth/overview#customizing-the-signup-process).
:::
## Authentication / Email / Overview
Wasp supports e-mail authentication out of the box, along with email verification and "forgot your password?" flows. It provides you with the server-side implementation and email templates for all of these flows.

:::caution[Using multiple auth identities for a single user]
Wasp currently doesn't support multiple auth identities for a single user. This means, for example, that a user can't have both an email-based auth identity and a Google-based auth identity. This is something we will add in the future with the introduction of the [account merging feature](https://github.com/wasp-lang/wasp/issues/954).
Account merging means that multiple auth identities can be merged into a single user account. For example, a user's email and Google identity can be merged into a single user account. Then the user can log in with either their email or Google account and they will be logged into the same account.
:::
### Setting Up Email Authentication
We'll need to take the following steps to set up email authentication:
1. Enable email authentication in the Wasp file
2. Add the `User` entity
3. Add the auth routes and pages
4. Use Auth UI components in our pages
5. Set up the email sender
Structure of the `main.wasp.ts` file we will end up with:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { SignupPage } from "./src/pages/auth" with { type: "ref" }
// Configuring e-mail authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
emailSender: {
// ...
},
spec: [
// Defining routes and pages
route("SignupRoute", "/signup", page(SignupPage)),
// ...
],
})
```
#### 1. Enable Email Authentication in `main.wasp.ts`
Let's start with adding the following to our `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the user entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable email authentication
email: {
// 3. Specify the email from field
fromField: {
name: "My App Postman",
email: "hello@itsme.com"
},
// 4. Specify the email verification and password reset options (we'll talk about them later)
emailVerification: {
clientRoute: "EmailVerificationRoute",
},
passwordReset: {
clientRoute: "PasswordResetRoute",
},
},
},
onAuthFailedRedirectTo: "/login",
onAuthSucceededRedirectTo: "/"
},
// ...
})
```
Read more about the `email` auth method options in the [`EmailAuthConfig` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailAuthConfig).
#### 2. Add the User Entity
The `User` entity can be as simple as including only the `id` field:
```prisma title="schema.prisma"
// 5. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
You can read more about how the `User` is connected to the rest of the auth system and how you can access the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities) section of the docs.
#### 3. Add the Routes and Pages
Next, we need to define the routes and pages for the authentication pages.
Add the following to the `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import {
LoginPage,
SignupPage,
RequestPasswordResetPage,
PasswordResetPage,
EmailVerificationPage,
} from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
route("SignupRoute", "/signup", page(SignupPage)),
route(
"RequestPasswordResetRoute",
"/request-password-reset",
page(RequestPasswordResetPage)
),
route("PasswordResetRoute", "/password-reset", page(PasswordResetPage)),
route("EmailVerificationRoute", "/email-verification", page(EmailVerificationPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 4. Create the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import {
LoginForm,
SignupForm,
VerifyEmailForm,
ForgotPasswordForm,
ResetPasswordForm,
} from "wasp/client/auth"
import { Link } from "react-router"
export function LoginPage() {
return (
Don't have an account yet? go to signup.
Forgot your password? reset it.
)
}
export function SignupPage() {
return (
I already have an account (go to login).
)
}
export function EmailVerificationPage() {
return (
If everything is okay, go to login
)
}
export function RequestPasswordResetPage() {
return (
)
}
export function PasswordResetPage() {
return (
If everything is okay, go to login
)
}
// A layout component to center the content
export function Layout({ children }: { children: React.ReactNode }) {
return (
{children}
)
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### 5. Set up an Email Sender
To support e-mail verification and password reset flows, we need an e-mail sender. Luckily, Wasp supports several email providers out of the box.
We'll use the `Dummy` provider to speed up the setup. It just logs the emails to the console instead of sending them. You can use any of the [supported email providers](https://wasp.sh/docs/advanced/email#providers).
To set up the `Dummy` provider to send emails, add the following to the `main.wasp.ts` file:
```ts title="main.wasp.ts"
export default app({
// ...
// 7. Set up the email sender
emailSender: {
provider: "Dummy",
},
})
```
#### Conclusion
That's it! We have set up email authentication in our app. ๐
Running `wasp db migrate-dev` and then `wasp start` should give you a working app with email authentication. If you want to put some of the pages behind authentication, read the [auth overview](https://wasp.sh/docs/auth/overview).
### Login and Signup Flows
#### Login

#### Signup

Some of the behavior you get out of the box:
1. Rate limiting
We are limiting the rate of sign-up requests to **1 request per minute** per email address. This is done to prevent spamming.
2. Preventing user email leaks
If somebody tries to signup with an email that already exists and it's verified, we *pretend* that the account was created instead of saying it's an existing account. This is done to prevent leaking the user's email address.
3. Allowing registration for unverified emails
If a user tries to register with an existing but **unverified** email, we'll allow them to do that. This is done to prevent bad actors from locking out other users from registering with their email address.
4. Password validation
Read more about the default password validation rules and how to override them in [auth overview docs](https://wasp.sh/docs/auth/overview).
### Email Verification Flow
:::info[Automatic email verification in development]
In development mode, you can skip the email verification step by setting the `SKIP_EMAIL_VERIFICATION_IN_DEV` environment variable to `true` in your `.env.server` file:
```env title=".env.server"
SKIP_EMAIL_VERIFICATION_IN_DEV=true
```
This is useful when you are developing your app and don't want to go through the email verification flow every time you sign up. It can be also useful when you are writing automated tests for your app.
:::
By default, Wasp requires the e-mail to be verified before allowing the user to log in. This is done by sending a verification email to the user's email address and requiring the user to click on a link in the email to verify their email address.
Our setup looks like this:
```ts title="main.wasp.ts"
// ...
emailVerification: {
clientRoute: "EmailVerificationRoute",
}
```
When the user receives an e-mail, they receive a link that goes to the client route specified in the `clientRoute` field. In our case, this is the `EmailVerificationRoute` route we defined in the `main.wasp.ts` file.
The content of the e-mail can be customized, read more about it in the [`EmailFlowConfig` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailFlowConfig#getemailcontentfn).
#### Email Verification Page
We defined our email verification page in the `auth.tsx` file.

### Password Reset Flow
Users can request a password and then they'll receive an e-mail with a link to reset their password.
Some of the behavior you get out of the box:
1. Rate limiting
We are limiting the rate of sign-up requests to **1 request per minute** per email address. This is done to prevent spamming.
2. Preventing user email leaks
If somebody requests a password reset with an unknown email address, we'll give back the same response as if the user requested a password reset successfully. This is done to prevent leaking information.
Our setup in `main.wasp.ts` looks like this:
```ts title="main.wasp.ts"
// ...
passwordReset: {
clientRoute: "PasswordResetRoute",
}
```
#### Request Password Reset Page
Users request their password to be reset by going to the `/request-password-reset` route. We defined our request password reset page in the `auth.tsx` file.

#### Password Reset Page
When the user receives an e-mail, they receive a link that goes to the client route specified in the `clientRoute` field. In our case, this is the `PasswordResetRoute` route we defined in the `main.wasp.ts` file.

Users can enter their new password there.
The content of the e-mail can be customized, read more about it in the [`EmailFlowConfig` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailFlowConfig#getemailcontentfn).
##### Password
- `ensurePasswordIsPresent(args)`
Checks if the password is present and throws an error if it's not.
- `ensureValidPassword(args)`
Checks if the password is valid and throws an error if it's not. Read more about the validation rules [here](https://wasp.sh/docs/auth/overview#default-validations).
### Using Auth
To read more about how to set up the logout button and how to get access to the logged-in user in our client and server code, read the [auth overview docs](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's email and other information like this:
```ts
const emailIdentity = user.identities.email
// Email address the user used to sign up, e.g. "fluffyllama@app.com".
emailIdentity.id
// `true` if the user has verified their email address.
emailIdentity.isEmailVerified
// Datetime when the email verification email was sent.
emailIdentity.emailVerificationSentAt
// Datetime when the last password reset email was sent.
emailIdentity.passwordResetSentAt
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
#### [Auth ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
[All the options for the auth field of the app spec, including userEntity.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Auth)
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailAuthConfig)
#### [EmailAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailAuthConfig)
[All the options for the email auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailAuthConfig)
Read more about the `userSignupFields` function in the [Auth Overview docs](https://wasp.sh/docs/auth/overview#signup-fields-customization).
## Authentication / Email / Create your own UI
When using the email auth provider, users log in with their email address and a password. On signup, Wasp validates the data and sends a verification email. The user account is not active until the user clicks the link in the verification email. Also, the user can reset their password through a similar flow.
:::tip
Read more about the default email and password validation rules in the [auth overview docs](https://wasp.sh/docs/auth/overview#default-validations).
:::
Even though Wasp offers premade [Auth UI](https://wasp.sh/docs/auth/ui) for your authentication flows, there are times when you might want more customization, so we also give you the option to create your own UI and call Wasp's auth actions from your own code, similar to how Auth UI does it under the hood.
### Example code
Below you can find a starting point for making your own UI in the client code. This example has all the necessary components to handle login, signup, email verification, and the password reset flow. You can customize any of its look and behaviour, just make sure to call the functions imported from `wasp/client/auth`.
```tsx title="src/pages/auth.tsx"
import {
login,
requestPasswordReset,
resetPassword,
signup,
verifyEmail,
} from 'wasp/client/auth'
import { useState } from 'react'
import { useNavigate } from 'react-router'
// This will be shown when the user wants to log in
export function LoginPage() {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const navigate = useNavigate()
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
await login({ email, password })
navigate('/')
} catch (error: unknown) {
setError(error as Error)
}
}
return (
)
}
// This will be shown when the user wants to sign up
export function SignupPage() {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const [needsConfirmation, setNeedsConfirmation] = useState(false)
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
await signup({ email, password })
setNeedsConfirmation(true)
} catch (error: unknown) {
console.error('Error during signup:', error)
setError(error as Error)
}
}
if (needsConfirmation) {
return (
Check your email for the confirmation link. If you don't see it, check
spam/junk folder.
)
}
return (
)
}
// This will be shown has clicked on the link in their
// email to verify their email address
export function EmailVerificationPage() {
const [error, setError] = useState(null)
const navigate = useNavigate()
async function handleClick() {
setError(null)
try {
// The token is passed as a query parameter
const token = new URLSearchParams(window.location.search).get('token')
if (!token) throw new Error('Token not found in URL')
await verifyEmail({ token })
navigate('/')
} catch (error: unknown) {
console.error('Error during email verification:', error)
setError(error as Error)
}
}
return (
<>
{error &&
Error: {error.message}
}
>
)
}
// This will be shown when the user wants to reset their password
export function RequestPasswordResetPage() {
const [email, setEmail] = useState('')
const [error, setError] = useState(null)
const [needsConfirmation, setNeedsConfirmation] = useState(false)
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
await requestPasswordReset({ email })
setNeedsConfirmation(true)
} catch (error: unknown) {
console.error('Error during requesting reset:', error)
setError(error as Error)
}
}
if (needsConfirmation) {
return (
Check your email for the confirmation link. If you don't see it, check
spam/junk folder.
)
}
return (
)
}
// This will be shown when the user clicks on the link in their
// email to reset their password
export function PasswordResetPage() {
const [error, setError] = useState(null)
const [newPassword, setNewPassword] = useState('')
const navigate = useNavigate()
async function handleSubmit(event: React.FormEvent) {
event.preventDefault()
setError(null)
try {
// The token is passed as a query parameter
const token = new URLSearchParams(window.location.search).get('token')
if (!token) throw new Error('Token not found in URL')
await resetPassword({ token, password: newPassword })
navigate('/')
} catch (error: unknown) {
console.error('Error during password reset:', error)
setError(error as Error)
}
}
return (
)
}
```
### API Reference
You can import the following functions from `wasp/client/auth`:
#### `login()`
An action for logging in the user. Make sure to do a redirect on success (e.g. to the main page of the app).
It takes one argument:
- `data: object` required
It has the following fields:
- `email: string` required
- `password: string` required
#### `signup()`
An action for signing up the user and starting the email verification. The user will not be logged in after this, as they still need to verify their email.
It takes one argument:
- `data: object` required
It has the following fields:
- `email: string` required
- `password: string` required
:::info
By default, Wasp will only save the `email` and `password` fields. If you want to add extra fields to your signup process, read about [defining extra signup fields](https://wasp.sh/docs/auth/overview#customizing-the-signup-process).
:::
#### `verifyEmail()`
An action for marking the email as valid and the user account as active. Make sure to do a redirect on success (e.g. to the login page).
It takes one argument:
- `data: object` required
It has the following fields:
- `token: string` required
The token that was created when signing up. It will be set as a URL Query Parameter named `token`.
#### `requestPasswordReset()`
An action for asking for a password reset email. This doesn't immediately reset their password, just sends the email.
It takes one argument:
- `data: object` required
It has the following fields:
- `email: string` required
#### `resetPassword()`
An action for confirming a password reset and providing the new password. Make sure to do a redirect on success (e.g. to the login page).
It takes one argument:
- `data: object` required
It has the following fields:
- `token: string` required
The token that was created when requesting the password reset. It will be set as a URL Query Parameter named `token`.
- `password: string` required
The new password for the user.
## Authentication / Social Auth / Overview
Social login options (e.g., *Log in with Google*) are a great (maybe even the best) solution for handling user accounts. A famous old developer joke tells us *"The best auth system is the one you never have to make."*
Wasp wants to make adding social login options to your app as painless as possible.
Using different social providers gives users a chance to sign into your app via their existing accounts on other platforms (Google, GitHub, etc.).
This page goes through the common behaviors between all supported social login providers and shows you how to customize them. It also gives an overview of Wasp's UI helpers - the quickest possible way to get started with social auth.
### Available Providers
Wasp currently supports the following social login providers:
#### [Google ยป](https://wasp.sh/docs/auth/social-auth/google)
[Users sign in with their Google account.](https://wasp.sh/docs/auth/social-auth/google)
#### [Github ยป](https://wasp.sh/docs/auth/social-auth/github)
[Users sign in with their Github account.](https://wasp.sh/docs/auth/social-auth/github)
#### [Keycloak ยป](https://wasp.sh/docs/auth/social-auth/keycloak)
[Users sign in with their Keycloak account.](https://wasp.sh/docs/auth/social-auth/keycloak)
#### [Slack ยป](https://wasp.sh/docs/auth/social-auth/slack)
[Users sign in with their Slack account.](https://wasp.sh/docs/auth/social-auth/slack)
#### [Discord ยป](https://wasp.sh/docs/auth/social-auth/discord)
[Users sign in with their Discord account.](https://wasp.sh/docs/auth/social-auth/discord)
#### [Microsoft ยป](https://wasp.sh/docs/auth/social-auth/microsoft)
[Users sign in with their Microsoft account.](https://wasp.sh/docs/auth/social-auth/microsoft)
Click on each provider for more details.
### User Entity
Wasp requires you to declare a `userEntity` for all `auth` methods (social or otherwise). This field tells Wasp which Entity represents the user.
Here's what the full setup looks like:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
google: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
}
```
### Default Behavior
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
If you wish to store more information about the user, you can override the default behavior. You can do this by defining the `userSignupFields` and `configFn` fields in `main.wasp.ts` for each provider.
You can create custom signup setups, such as allowing users to define a custom username after they sign up with a social provider.
#### Example: Allowing User to Set Their Username
If you want to modify the signup flow (e.g., let users choose their own usernames), you will need to go through three steps:
1. The first step is adding a `isSignupComplete` property to your `User` Entity. This field will signal whether the user has completed the signup process.
2. The second step is overriding the default signup behavior.
3. The third step is implementing the rest of your signup flow and redirecting users where appropriate.
Let's go through both steps in more detail.
##### 1. Adding the `isSignupComplete` Field to the `User` Entity
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String? @unique
isSignupComplete Boolean @default(false)
}
```
##### 2. Overriding the Default Behavior
Declare an import under `auth.methods.google.userSignupFields` (the example assumes you're using Google):
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { userSignupFields } from "./src/auth/google" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
google: {
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
And implement the imported function:
```ts title="src/auth/google.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
isSignupComplete: () => false,
});
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
##### 3. Showing the Correct State on the Client
You can check the `isSignupComplete` flag on the `user` object. Authenticated pages come with the [`user` prop](https://wasp.sh/docs/auth/overview#getting-the-user-in-authenticated-routes) which gives you access to the current user. If the `user` prop is out of reach, fetch the current user with the [`useAuth()` hook](https://wasp.sh/docs/auth/overview#getting-the-user-in-non-authenticated-routes).
Depending on the flag's value, you can redirect users to the appropriate signup step.
For example:
1. When the user lands on the homepage, check the value of `user.isSignupComplete`.
2. If it's `false`, it means the user has started the signup process but hasn't yet chosen their username. Therefore, you can redirect them to `EditUserDetailsPage` where they can edit the `username` property.
```tsx title="src/HomePage.tsx"
import { Navigate } from "react-router";
import type { AuthUser } from "wasp/auth";
export function HomePage({ user }: { user: AuthUser }) {
if (user.isSignupComplete === false) {
return ;
}
// ...
}
```
The same general principle applies to more complex signup procedures, just change the boolean `isSignupComplete` property to a property like `currentSignupStep` that can hold more values.
#### Using the User's Provider Account Details
Account details are provider-specific. Each provider has their own rules for defining the `userSignupFields` and `configFn` fields:
#### [Google ยป](https://wasp.sh/docs/auth/social-auth/google#overrides)
[Users sign in with their Google account.](https://wasp.sh/docs/auth/social-auth/google#overrides)
#### [Github ยป](https://wasp.sh/docs/auth/social-auth/github#overrides)
[Users sign in with their Github account.](https://wasp.sh/docs/auth/social-auth/github#overrides)
#### [Keycloak ยป](https://wasp.sh/docs/auth/social-auth/keycloak#overrides)
[Users sign in with their Keycloak account.](https://wasp.sh/docs/auth/social-auth/keycloak#overrides)
#### [Slack ยป](https://wasp.sh/docs/auth/social-auth/slack#overrides)
[Users sign in with their Slack account.](https://wasp.sh/docs/auth/social-auth/slack#overrides)
#### [Discord ยป](https://wasp.sh/docs/auth/social-auth/discord#overrides)
[Users sign in with their Discord account.](https://wasp.sh/docs/auth/social-auth/discord#overrides)
#### [Microsoft ยป](https://wasp.sh/docs/auth/social-auth/microsoft#overrides)
[Users sign in with their Microsoft account.](https://wasp.sh/docs/auth/social-auth/microsoft#overrides)
Click on each provider for more details.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the social auth methods.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For more information on the provider-specific behavior of the `userSignupFields` and `configFn` functions, check the provider-specific API References:
#### [Google ยป](https://wasp.sh/docs/auth/social-auth/google#api-reference)
[Users sign in with their Google account.](https://wasp.sh/docs/auth/social-auth/google#api-reference)
#### [Github ยป](https://wasp.sh/docs/auth/social-auth/github#api-reference)
[Users sign in with their Github account.](https://wasp.sh/docs/auth/social-auth/github#api-reference)
#### [Keycloak ยป](https://wasp.sh/docs/auth/social-auth/keycloak#api-reference)
[Users sign in with their Keycloak account.](https://wasp.sh/docs/auth/social-auth/keycloak#api-reference)
#### [Slack ยป](https://wasp.sh/docs/auth/social-auth/slack#api-reference)
[Users sign in with their Slack account.](https://wasp.sh/docs/auth/social-auth/slack#api-reference)
#### [Discord ยป](https://wasp.sh/docs/auth/social-auth/discord#api-reference)
[Users sign in with their Discord account.](https://wasp.sh/docs/auth/social-auth/discord#api-reference)
#### [Microsoft ยป](https://wasp.sh/docs/auth/social-auth/microsoft#api-reference)
[Users sign in with their Microsoft account.](https://wasp.sh/docs/auth/social-auth/microsoft#api-reference)
Click on each provider for more details.
## Authentication / Social Auth / GitHub
Wasp supports GitHub Authentication out of the box. GitHub is a great external auth choice when you're building apps for developers, as most of them already have a GitHub account.
Letting your users log in using their GitHub accounts turns the signup process into a breeze.
Let's walk through enabling GitHub Authentication, explain some of the default settings, and show how to override them.
### Setting up GitHub Auth
Enabling GitHub Authentication comes down to a series of steps:
1. Enabling GitHub authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating a GitHub OAuth app.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Adding GitHub Auth to Your Wasp File
Let's start by properly configuring the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable GitHub Auth
gitHub: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
#### 2. Add the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a GitHub OAuth App
To use GitHub as an authentication method, you'll first need to create a GitHub App and provide Wasp with your client key and secret. Here's how you do it:
1. Create a GitHub account if you do not already have one: .
2. Create and configure a new GitHub App: .

3. We have to fill out **App name** and **Homepage URL** fields. Additionally we will add our authorization **Callback URL**s:
- For development, put: `http://localhost:3001/auth/github/callback`.
- Once you know your production URL you can add it via the **Add Callback URL** button, e.g. `https://your-server-url.com/auth/github/callback`.

4. We will turn off the **Active** checkbox under the Webhook title, and then we can click **Create GitHub App**.

5. Finally, we can click **Generate a new client secret** button and then copy both **Client ID** and **Client secret**.

#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
```
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Creating the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up GitHub Auth! ๐
*(Inlined image: GitHub Auth)*
Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Default Behaviour
Add `gitHub: {}` to the `auth.methods` object to use it with default settings.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
gitHub: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From GitHub
We are using GitHub's API and its `/user` and `/user/emails` endpoints to get the user data.
:::info[We combine the data from the two endpoints]
You'll find the emails in the `emails` property in the object that you receive in `userSignupFields`.
This is because we combine the data from the `/user` and `/user/emails` endpoints **if the `user` or `user:email` scope is requested.**
:::
The data we receive from GitHub on the `/user` endpoint looks something this:
```json
{
"login": "octocat",
"id": 1,
"name": "monalisa octocat",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": ""
// ...
}
```
And the data from the `/user/emails` endpoint looks something like this:
```json
[
{
"email": "octocat@github.com",
"verified": true,
"primary": true,
"visibility": "public"
}
]
```
The fields you receive will depend on the scopes you requested. By default we don't specify any scopes. If you want to get the emails, you need to specify the `user` or `user:email` scope in the `configFn` function.
For an up to date info about the data received from GitHub, please refer to the [GitHub API documentation](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user).
#### Using the Data Received From GitHub
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { getConfig, userSignupFields } from "./src/auth/github" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
gitHub: {
configFn: getConfig,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
displayName String
}
// ...
```
```ts title="src/auth/github.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
username: () => "hardcoded-username",
displayName: (data: any) => data.profile.name,
});
export function getConfig() {
return {
scopes: ["user"],
};
}
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's GitHub ID like this:
```ts
const githubIdentity = user.identities.github
// GitHub User ID for example "12345678"
githubIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the gitHub auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Google
Wasp supports Google Authentication out of the box. Google Auth is arguably the best external auth option, as most users on the web already have Google accounts.
Enabling it lets your users log in using their existing Google accounts, greatly simplifying the process and enhancing the user experience.
Let's walk through enabling Google authentication, explain some of the default settings, and show how to override them.
### Setting up Google Auth
Enabling Google Authentication comes down to a series of steps:
1. Enabling Google authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating a Google OAuth app.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Adding Google Auth to Your Wasp File
Let's start by properly configuring the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable Google Auth
google: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
`userEntity` is explained in [the social auth overview](https://wasp.sh/docs/auth/social-auth/overview#user-entity).
#### 2. Adding the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a Google OAuth App
To use Google as an authentication method, you'll first need to create a Google project and provide Wasp with your client key and secret. Here's how you do it:
1. Create a Google Cloud Platform account if you do not already have one:
2. Create and configure a new Google project here:


3. Search for **Google Auth** in the top bar (1), click on **Google Auth Platform** (2). Then click on **Get Started** (3).


4. Fill out you app information. For the **Audience** field, we will go with **External**. When you're done, click **Create**.


5. You should now be in the **OAuth Overview** page. Click on **Create OAuth Client** (1).

6. Fill out the form. These are the values for a typical Wasp application:
| # | Field | Value |
| - | ------------------------ | -------------------------------------------- |
| 1 | Application type | Web application |
| 2 | Name | (your wasp app name) |
| 3 | Authorized redirect URIs | `http://localhost:3001/auth/google/callback` |
:::note
Once you know on which URL(s) your API server will be deployed, also add those URL(s) to the **Authorized redirect URIs**.\
For example: `https://your-server-url.com/auth/google/callback`
:::

Then click on **Create** (4).
7. You will see a box saying **OAuth client created**. Click on **OK**.

8. Click on the name of your newly-created app.

9. On the right-hand side, you will see your **Client ID** (1) and **Client secret** (2). **Copy them somewhere safe, as you will need them for your app.**

:::info
These are the credentials your app will use to authenticate with Google. Do not share them anywhere publicly, as anyone with these credentials can impersonate your app and access user data.
:::
10. Click on **Data Access** (1) in the left-hand menu, then click on **Add or remove scopes** (2). You should select `userinfo.profile` (3), and optionally `userinfo.email` (4), or any other scopes you want to use. Remember to click **Update** and **Save** when done.


11. Go to **Audience** (1) in the left-hand menu, and add any test users you want (2). This is useful for testing your app before going live. You can add any email addresses you want to test with.

12. Finally, you can go to **Branding** (1) in the left-hand menu, and customize your app's branding in the Google login page. *This is optional*, but recommended if you want to make your app look more professional.

#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
```
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Create the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up Google Auth! ๐
*(Inlined image: Google Auth)*
Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Default Behaviour
Add `google: {}` to the `auth.methods` object to use it with default settings:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
google: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From Google
We are using Google's API and its `/userinfo` endpoint to fetch the user's data.
The data received from Google is an object which can contain the following fields:
```json
[
"name",
"given_name",
"family_name",
"email",
"email_verified",
"aud",
"exp",
"iat",
"iss",
"locale",
"picture",
"sub"
]
```
The fields you receive depend on the scopes you request. The default scope is set to `profile` only. If you want to get the user's email, you need to specify the `email` scope in the `configFn` function.
For an up to date info about the data received from Google, please refer to the [Google API documentation](https://developers.google.com/identity/openid-connect/openid-connect#an-id-tokens-payload).
#### Using the Data Received From Google
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { getConfig, userSignupFields } from "./src/auth/google" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
google: {
configFn: getConfig,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
displayName String
}
// ...
```
```ts title="src/auth/google.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
username: () => "hardcoded-username",
displayName: (data: any) => data.profile.name,
});
export function getConfig() {
return {
scopes: ["profile", "email"],
};
}
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's Google ID like this:
```ts
const googleIdentity = user.identities.google
// Google User ID for example "123456789012345678901"
googleIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the google auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Keycloak
Wasp supports Keycloak Authentication out of the box.
[Keycloak](https://www.keycloak.org/) is an open-source identity and access management solution for modern applications and services. Keycloak provides both SAML and OpenID protocol solutions. It also has a very flexible and powerful administration UI.
Let's walk through enabling Keycloak authentication, explain some of the default settings, and show how to override them.
### Setting up Keycloak Auth
Enabling Keycloak Authentication comes down to a series of steps:
1. Enabling Keycloak authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating a Keycloak client.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Adding Keycloak Auth to Your Wasp File
Let's start by properly configuring the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable Keycloak Auth
keycloak: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
`userEntity` is explained in [the social auth overview](https://wasp.sh/docs/auth/social-auth/overview#user-entity).
#### 2. Adding the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a Keycloak Client
1. Log into your Keycloak admin console.
2. Under **Clients**, click on **Create Client**.

1. Fill in the **Client ID** and choose a name for the client.

1. In the next step, enable **Client Authentication**.

1. Under **Valid Redirect URIs**, add `http://localhost:3001/auth/keycloak/callback` for local development.

- Once you know on which URL(s) your API server will be deployed, also add those URL(s).
- For example: `https://my-server-url.com/auth/keycloak/callback`.
1. Click **Save**.
2. In the **Credentials** tab, copy the **Client Secret** value, which we'll use in the next step.

#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
KEYCLOAK_CLIENT_ID=your-keycloak-client-id
KEYCLOAK_CLIENT_SECRET=your-keycloak-client-secret
KEYCLOAK_REALM_URL=https://your-keycloak-url.com/realms/master
```
We assumed in the `KEYCLOAK_REALM_URL` env variable that you are using the `master` realm. If you are using a different realm, replace `master` with your realm name.
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Create the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up Keycloak Auth!
Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Default Behaviour
Add `keycloak: {}` to the `auth.methods` object to use it with default settings:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
keycloak: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From Keycloak
We are using Keycloak's API and its `/userinfo` endpoint to fetch the user's data.
```ts title="Keycloak user data"
{
sub: "5adba8fc-3ea6-445a-a379-13f0bb0b6969",
email_verified: true,
name: "Test User",
preferred_username: "test",
given_name: "Test",
family_name: "User",
email: "test@example.com"
}
```
The fields you receive will depend on the scopes you requested. The default scope is set to `profile` only. If you want to get the user's email, you need to specify the `email` scope in the `configFn` function.
For up-to-date info about the data received from Keycloak, please refer to the [Keycloak API documentation](https://www.keycloak.org/docs-api/23.0.7/javadocs/org/keycloak/representations/UserInfo.html).
#### Using the Data Received From Keycloak
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { getConfig, userSignupFields } from "./src/auth/keycloak" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
keycloak: {
configFn: getConfig,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
displayName String
}
// ...
```
```ts title="src/auth/keycloak.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
username: () => "hardcoded-username",
displayName: (data: any) => data.profile.name,
});
export function getConfig() {
return {
scopes: ["profile", "email"],
};
}
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's Keycloak ID like this:
```ts
const keycloakIdentity = user.identities.keycloak
// Keycloak User ID for example "12345678-1234-1234-1234-123456789012"
keycloakIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the keycloak auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Slack
Wasp supports Slack Authentication out of the box.
Using Slack Authentication is perfect when you build a control panel for a Slack app.
Let's walk through enabling Slack Authentication, explain some quirks, explore default settings and show how to override them.
### Setting up Slack Auth
Enabling Slack Authentication comes down to a series of steps:
1. Enabling Slack authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating Slack App.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Enabling Slack authentication in the Wasp file.
Now let's properly configure the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable Slack Auth
slack: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
#### 2. Add the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a Slack App
To use Slack as an authentication method, you'll first need to create a Slack App and provide Wasp with your client key and secret. Here's how you do it:
1. Log into your Slack account and navigate to: .
2. Select **Create New App**.
3. Click "From scratch"
4. Enter App Name and select workspace that should host your app.

4. Go to the **OAuth & Permissions** tab on the sidebar and click **Add New Redirect URL**.
- Enter the value `https://.local.lt/auth/slack/callback`, where `` is your selected localtunnel subdomain.
- Slack requires us to use HTTPS even when developing, [read below](#slack-https) how to set it up.
5. Hit **Save URLs**.
6. Go to **Basic Information** tab
7. Hit **Show** next to **Client Secret**
8. Copy your Client ID and Client Secret as you'll need them in the next step.
:::tip
Be precise with your redirect URL. Slackโs redirect URLs are case-sensitive and sensitive to trailing slashes. For example, `https://your-app.loca.lt/auth/slack/callback` and `https://your-app.loca.lt/auth/slack/callback/` are **not** the same.
:::
#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
SLACK_CLIENT_ID=your-slack-client-id
SLACK_CLIENT_SECRET=your-slack-client-secret
```
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Creating the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up Slack Auth! ๐

Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Developing with Slack auth and HTTPS {#slack-https}
Unlike most OAuth providers, Slack **requires HTTPS and publicly accessible URL for the OAuth redirect URL**. This means that we can't simply use `localhost:3001` as a base host for redirect urls. Instead, we need to configure Wasp server to be publicly available under HTTPS, even in the local development environment.
Fortunately, there are quite a few free and convenient tools available to simplify the process, such as [localtunnel.me](https://localtunnel.me/) (free) and [ngrok.com](https://ngrok.com) (lots of features, but free tier is limited).
Using localtunnel
Install localtunnel globally with `npm install -g localtunnel`.
Start a tunnel with `lt --port 3001 -s `, where `` is a unique subdomain you would like to have.
:::info[Subdomain option]
Usually localtunnel will assign you a random subdomain on each start, but you can specify it with the `-s` flag. Doing it this way will make it easier to remember the URL and will also make it easier to set up the redirect URL in Slack app settings.
:::
After starting the tunnel, you will see your tunnel URL in the terminal. Go to that URL to unlock the tunnel by entering your IP address in a field that appears on the page the first time you open it in the browser. This is a basic anti-abuse mechanism. If you're not sure what your IP is, you can find it by running `curl ifconfig.me` or going to [ifconfig.me](https://ifconfig.me).
Now that your server is exposed to the public, we need to configure Wasp to use the new public domain. This needs to be done in two places: server and client configuration.
To configure client, add this line to your `.env.client` file (create it if doesn't exist):
```bash title=".env.client"
REACT_APP_API_URL=https://.loca.lt
```
Similarly, to configure the server, add this line to your `.env.server`:
```bash title=".env.server"
WASP_SERVER_URL=https://.loca.lt
```
### Default Behaviour
Add `slack: {}` to the `auth.methods` object to use it with default settings.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
slack: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From Slack
We are using Slack's API and its `/openid.connect.userInfo` endpoint to get the user data.
The data we receive from Slack on the `/openid.connect.userInfo` endpoint looks something like this:
```json
{
"ok": true,
"sub": "U0R7JM",
"https://slack.com/user_id": "U0R7JM",
"https://slack.com/team_id": "T0R7GR",
"email": "krane@slack-corp.com",
"email_verified": true,
"date_email_verified": 1622128723,
"name": "krane",
"picture": "https://secure.gravatar.com/....png",
"given_name": "Bront",
"family_name": "Labradoodle",
"locale": "en-US",
"https://slack.com/team_name": "kraneflannel",
"https://slack.com/team_domain": "kraneflannel",
"https://slack.com/user_image_24": "...",
"https://slack.com/user_image_32": "...",
"https://slack.com/user_image_48": "...",
"https://slack.com/user_image_72": "...",
"https://slack.com/user_image_192": "...",
"https://slack.com/user_image_512": "...",
"https://slack.com/team_image_34": "...",
"https://slack.com/team_image_44": "...",
"https://slack.com/team_image_68": "...",
"https://slack.com/team_image_88": "...",
"https://slack.com/team_image_102": "...",
"https://slack.com/team_image_132": "...",
"https://slack.com/team_image_230": "...",
"https://slack.com/team_image_default": true
}
```
The fields you receive depend on the scopes you request. In the example above, the scope includes `email`, `profile` and `openid`. By default, only `openid` is requested. See below for instructions on how to request additional scopes.
For an up to date info about the data received from Slack, please refer to the [Slack API documentation](https://api.slack.com/methods/openid.connect.userInfo).
#### Using the Data Received From Slack
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { config, userSignupFields } from "./src/auth/slack" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
slack: {
configFn: config,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
avatarUrl String
}
// ...
```
```ts title="src/auth/slack.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export function config() {
console.log("Inside user-supplied Slack config");
return {
scopes: ["openid", "email", "profile"],
};
}
export const userSignupFields = defineUserSignupFields({
username: (data: any) => data.profile.name,
avatarUrl: (data: any) => data.profile.picture,
});
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's Slack ID like this:
```ts
const slackIdentity = user.identities.slack
// Discord User ID for example "80351110224678912"
slackIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the slack auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Discord
Wasp supports Discord Authentication out of the box.
Letting your users log in using their Discord accounts turns the signup process into a breeze.
Let's walk through enabling Discord Authentication, explain some of the default settings, and show how to override them.
### Setting up Discord Auth
Enabling Discord Authentication comes down to a series of steps:
1. Enabling Discord authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating a Discord App.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Adding Discord Auth to Your Wasp File
Let's start by properly configuring the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable Discord Auth
discord: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
#### 2. Add the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a Discord App
To use Discord as an authentication method, you'll first need to create a Discord App and provide Wasp with your client key and secret. Here's how you do it:
1. Log into your Discord account and navigate to: .
2. Select **New Application**.
3. Supply required information.

4. Go to the **OAuth2** tab on the sidebar and click **Add Redirect**
- For development, put: `http://localhost:3001/auth/discord/callback`.
- Once you know on which URL your API server will be deployed, you can create a new app with that URL instead e.g. `https://your-server-url.com/auth/discord/callback`.
4. Hit **Save Changes**.
5. Hit **Reset Secret**.
6. Copy your Client ID and Client secret as you'll need them in the next step.
#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
DISCORD_CLIENT_ID=your-discord-client-id
DISCORD_CLIENT_SECRET=your-discord-client-secret
```
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Creating the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up Discord Auth! ๐
*(Inlined image: Discord Auth)*
Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Default Behaviour
Add `discord: {}` to the `auth.methods` object to use it with default settings.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
discord: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From Discord
We are using Discord's API and its `/users/@me` endpoint to get the user data.
The data we receive from Discord on the `/users/@me` endpoint looks something like this:
```json
{
"id": "80351110224678912",
"username": "Nelly",
"discriminator": "1337",
"avatar": "8342729096ea3675442027381ff50dfe",
"verified": true,
"flags": 64,
"banner": "06c16474723fe537c283b8efa61a30c8",
"accent_color": 16711680,
"premium_type": 1,
"public_flags": 64,
"avatar_decoration_data": {
"sku_id": "1144058844004233369",
"asset": "a_fed43ab12698df65902ba06727e20c0e"
}
}
```
The fields you receive will depend on the scopes you requested. The default scope is set to `identify` only. If you want to get the email, you need to specify the `email` scope in the `configFn` function.
For an up to date info about the data received from Discord, please refer to the [Discord API documentation](https://discord.com/developers/docs/resources/user#user-object-user-structure).
#### Using the Data Received From Discord
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { getConfig, userSignupFields } from "./src/auth/discord" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
discord: {
configFn: getConfig,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
displayName String
}
// ...
```
```ts title="src/auth/discord.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
username: (data: any) => data.profile.global_name,
avatarUrl: (data: any) => data.profile.avatar,
});
export function getConfig() {
return {
scopes: ["identify"],
};
}
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's Discord ID like this:
```ts
const discordIdentity = user.identities.discord
// Discord User ID for example "80351110224678912"
discordIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the discord auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Microsoft
Wasp supports Microsoft Authentication out of the box.
Microsoft Auth uses [Microsoft Entra ID](https://www.microsoft.com/en-us/security/business/identity-access/microsoft-entra-id) as the identity provider. This lets your users sign in with their Microsoft accounts โ including personal Microsoft accounts, work/school (Microsoft 365) accounts, or both, depending on your configuration.
Let's walk through enabling Microsoft authentication, explain some of the default settings, and show how to override them.
### Understanding tenants
When setting up Microsoft Authentication, you'll encounter the concept of "tenants". A tenant represents an organization in Microsoft Entra ID. Depending on the supported account types you choose during app registration, your app will be associated with a specific tenant, or be multi-tenant.
When planning out your Microsoft integration, you have to decide which kind of accounts will be able to sign in. This will determine the tenant ID you use in your configuration, and the users that can sign in to your app.
| Supported Account Types | Tenant ID Value | Description | Best for |
| ----------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------------- |
| Tenant-specific | Your Tenant ID (generated by Microsoft) | Only users in your Microsoft Entra ID tenant can sign in. | Internal-only apps for a specific organization. |
| Organization accounts | `organizations` | Any user with a "work or school" Microsoft account can sign in, but not personal accounts. | B2B apps targeting users in other organizations. |
| Personal accounts | `consumers` | Only personal Microsoft accounts (e.g., Outlook.com, Hotmail, Xbox) can sign in. | Consumer-facing apps targeting individual users. |
| All of the above | `common` | Both organizational accounts and personal Microsoft accounts can sign in. | Apps that can be used by personal, work, and school accounts. |
We recommend choosing the least-permissive option that fits your use case, as Microsoft might require you to submit to a verification process if you want to use the higher-privileged tenant types in production.
### Setting up Microsoft Auth
Enabling Microsoft Authentication comes down to a series of steps:
1. Enabling Microsoft authentication in the Wasp file.
2. Adding the `User` entity.
3. Creating a Microsoft Entra ID app registration.
4. Adding the necessary Routes and Pages
5. Using Auth UI components in our Pages.
Here's a skeleton of what our `main.wasp.ts` should look like after we're done:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
// Configuring the social authentication
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
},
spec: [
// Defining routes and pages
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
#### 1. Adding Microsoft Auth to Your Wasp File
Let's start by properly configuring the Auth object:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// 1. Specify the User entity (we'll define it next)
userEntity: "User",
methods: {
// 2. Enable Microsoft Auth
microsoft: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
`userEntity` is explained in [the social auth overview](https://wasp.sh/docs/auth/social-auth/overview#user-entity).
#### 2. Adding the User Entity
Let's now define the `auth.userEntity` entity in the `schema.prisma` file:
```prisma title="schema.prisma"
// 3. Define the user entity
model User {
id Int @id @default(autoincrement())
// Add your own fields below
// ...
}
```
#### 3. Creating a Microsoft Entra ID App Registration
To use Microsoft as an authentication method, you'll first need to register an application in the Microsoft Entra ID portal and provide Wasp with your client ID, client secret, and tenant ID.
1. Go to the [Microsoft Entra ID portal](https://entra.microsoft.com/). Login or sign up if necessary.
2. In the left sidebar, click on "App registrations" **(1)** and then "New registration" **(2)**.

3. Fill out the form. These are the values for a typical Wasp application:
| # | Field | Value |
| - | ------------------------ | ---------------------------------------------------------------- |
| 1 | Name | (your wasp app name) |
| 1 | Supported account types | Read through **[Understanding tenants](#understanding-tenants)** |
| 3 | Authorized redirect URIs | Web: `http://localhost:3001/auth/microsoft/callback` |
:::note
Once you know on which URL(s) your API server will be deployed, also add those URL(s) in the **Authentication** section.\
For example: `https://your-server-url.com/auth/microsoft/callback`
:::

4. You should now see your app registration's overview page. Take note of the **Application (client) ID (1)** and the **Directory (tenant) ID (2)**, as you'll need them in the next steps.

5. Next, go to the "Certificates & secrets" **(1)** page from the left sidebar, and create a new client secret **(2)**. You can fill out the name of your Wasp app as the client secret's name **(3)**.

6. Finally, take note of the client secret's value, as you'll need it in the next steps. Make sure to copy it somewhere safe, as you won't be able to see it again!

:::info
The keys you copied are the credentials your app will use to authenticate with Microsoft. Do not share them anywhere publicly, as anyone with these credentials can impersonate your app and access user data.
:::
#### 4. Adding Environment Variables
Add these environment variables to the `.env.server` file at the root of your project (take their values from the previous step):
```bash title=".env.server"
MICROSOFT_TENANT_ID=your-microsoft-tenant-id
MICROSOFT_CLIENT_ID=your-microsoft-client-id
MICROSOFT_CLIENT_SECRET=your-microsoft-client-secret
```
The `MICROSOFT_TENANT_ID` should be set based on the supported account types you chose, as described in the [Understanding Tenant IDs](#understanding-tenants) section above.
#### 5. Adding the Necessary Routes and Pages
Let's define the necessary authentication Routes and Pages.
Add the following code to your `main.wasp.ts` file:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LoginPage } from "./src/pages/auth" with { type: "ref" }
export default app({
// ...
spec: [
route("LoginRoute", "/login", page(LoginPage)),
],
})
```
We'll define the React components for these pages in the `src/pages/auth.tsx` file below.
#### 6. Create the Client Pages
:::info
We are using [Tailwind CSS](https://tailwindcss.com/) to style the pages. Read more about how to add it [here](https://wasp.sh/docs/guides/libraries/tailwind).
:::
Let's create a `auth.tsx` file in the `src/pages` folder and add the following to it:
```tsx title="src/pages/auth.tsx"
import type { ReactNode } from "react";
import { LoginForm } from "wasp/client/auth";
export function LoginPage() {
return (
);
}
// A layout component to center the content
export function Layout({ children }: { children: ReactNode }) {
return (
{children}
);
}
```
We imported the generated Auth UI components and used them in our pages. Read more about the Auth UI components [here](https://wasp.sh/docs/auth/ui).
#### Conclusion
Yay, we've successfully set up Microsoft Auth! ๐

Running `wasp db migrate-dev` and `wasp start` should now give you a working app with authentication. To see how to protect specific pages (i.e., hide them from non-authenticated users), read the docs on [using auth](https://wasp.sh/docs/auth/overview).
### Default Behaviour
Add `microsoft: {}` to the `auth.methods` object to use it with default settings:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
microsoft: {}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
When a user **signs in for the first time**, Wasp creates a new user account and links it to the chosen auth provider account for future logins.
### Overrides
By default, Wasp doesn't store any information it receives from the social login provider. It only stores the user's ID specific to the provider.
There are two mechanisms used for overriding the default behavior:
- `userSignupFields`
- `configFn`
Let's explore them in more detail.
#### Data Received From Microsoft
We are using Microsoft's Graph API and its `/oidc/userinfo` endpoint to fetch the user's data.
The data received from Microsoft is an object which can contain the following fields:
```json
{
"sub": "OLu859SGc2Sr9ZsqbkG-QbeLgJlb41KcdiPoLYNpSFA",
"name": "Mikah Ollenburg", // all names require the โprofileโ scope.
"family_name": " Ollenburg",
"given_name": "Mikah",
"picture": "https://graph.microsoft.com/v1.0/me/photo/$value",
"email": "mikoll@contoso.com" // requires the โemailโ scope.
}
```
The fields you receive depend on the scopes you request. The default scopes are set to `openid`, `profile`, and `email`.
For up-to-date info about the data received from Microsoft, please refer to the [Microsoft Graph API documentation](https://learn.microsoft.com/en-us/entra/identity-platform/userinfo).
#### Using the Data Received From Microsoft
When a user logs in using a social login provider, the backend receives some data about the user. Wasp lets you access this data inside the `userSignupFields` getters.
For example, the User entity can include a `displayName` field which you can set based on the details received from the provider.
Wasp also lets you customize the configuration of the providers' settings using the `configFn` function.
Let's use this example to show both fields in action:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { getConfig, userSignupFields } from "./src/auth/microsoft" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
microsoft: {
configFn: getConfig,
userSignupFields
}
},
onAuthFailedRedirectTo: "/login"
},
// ...
})
```
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
username String @unique
displayName String
}
// ...
```
```ts title="src/auth/microsoft.ts"
import { defineUserSignupFields } from "wasp/server/auth";
export const userSignupFields = defineUserSignupFields({
username: () => "hardcoded-username",
displayName: (data: any) => data.profile.name,
});
export function getConfig() {
return {
scopes: ["openid", "profile", "email"],
};
}
```
Wasp automatically generates the `defineUserSignupFields` function to help you correctly type your `userSignupFields` object.
### Using Auth
To read more about how to set up the logout button and get access to the logged-in user in both client and server code, read the docs on [using auth](https://wasp.sh/docs/auth/overview).
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), you'll be able to access the user's Microsoft ID like this:
```ts
const microsoftIdentity = user.identities.microsoft
// Microsoft User ID for example "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
microsoftIdentity.id
```
Read more about accessing the user data in the [Accessing User Data](https://wasp.sh/docs/auth/entities#accessing-the-auth-fields) section of the docs.
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
#### [SocialAuthConfig ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
[All the options for the microsoft auth method.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/SocialAuthConfig)
For the provider-specific behavior of the `configFn` and `userSignupFields` functions, check the [Overrides section](#overrides). For behavior common to all providers, check the [Social Auth Overview](https://wasp.sh/docs/auth/social-auth/overview).
## Authentication / Social Auth / Create your own UI
[Auth UI](https://wasp.sh/docs/auth/ui) is a common name for all high-level auth forms that come with Wasp.
These include fully functional auto-generated login and signup forms with working social login buttons. If you're looking for the fastest way to get your auth up and running, that's where you should look.
The UI helpers described below are lower-level and are useful for creating your custom login links.
Wasp provides sign-in buttons and URLs for each of the supported social login providers.
```tsx title="src/LoginPage.tsx"
import {
GoogleSignInButton,
googleSignInUrl,
GitHubSignInButton,
githubSignInUrl,
} from "wasp/client/auth";
export const LoginPage = () => {
return (
<>
{/* or */}
Sign in with GoogleSign in with GitHub
>
);
};
```
## Authentication / Accessing User Data
First, we'll check out the most practical info: **how to access the user's data in your app**.
Then, we'll dive into the details of the **auth entities** that Wasp creates behind the scenes to store the user's data. For auth each method, Wasp needs to store different information about the user. For example, username for [Username & password](https://wasp.sh/docs/auth/username-and-pass) auth, email verification status for [Email](https://wasp.sh/docs/auth/email) auth, and so on.
We'll also show you how you can use these entities to create a custom signup action.
### Accessing the Auth Fields
When you receive the `user` object [on the client or the server](https://wasp.sh/docs/auth/overview#accessing-the-logged-in-user), it will contain all the user fields you defined in the `User` entity in the `schema.prisma` file. In addition to that, it will also contain all the auth-related fields that Wasp stores. This includes things like the `username` or the email verification status. In Wasp, this data is called the `AuthUser` object.
#### `AuthUser` Object Fields
All the `User` fields you defined will be present at the top level of the `AuthUser` object. The auth-related fields will be on the `identities` object. For each auth method you enable, there will be a separate data object in the `identities` object.
The `AuthUser` object will change depending on which auth method you have enabled in the Wasp file. For example, if you enabled the email auth and Google auth, it would look something like this:
**User Signed Up with Google**
If the user has only the Google identity, the `AuthUser` object will look like this:
```ts
const user = {
// User data
id: "cluqs9qyh00007cn73apj4hp7",
address: "Some address",
// Auth methods specific data
identities: {
email: null,
google: {
id: "1117XXXX1301972049448",
},
},
}
```
**User Signed Up with Email**
If the user has only the email identity, the `AuthUser` object will look like this:
```ts
const user = {
// User data
id: "cluqsex9500017cn7i2hwsg17",
address: "Some address",
// Auth methods specific data
identities: {
email: {
id: "user@app.com",
isEmailVerified: true,
emailVerificationSentAt: "2024-04-08T10:06:02.204Z",
passwordResetSentAt: null,
},
google: null,
},
}
```
In the examples above, you can see the `identities` object contains the `email` and `google` objects. The `email` object contains the email-related data and the `google` object contains the Google-related data.
:::info[Make sure to check if the data exists]
Before accessing some auth method's data, you'll need to check if that data exists for the user and then access it:
```ts
if (user.identities.google !== null) {
const userId = user.identities.google.id
// ...
}
```
You need to do this because if a user didn't sign up with some auth method, the data for that auth method will be `null`.
:::
Let's look at the data for each of the available auth methods:
- [Username & password](https://wasp.sh/docs/auth/username-and-pass) data
```ts
const usernameIdentity = user.identities.username
// Username that the user used to sign up, e.g. "fluffyllama"
usernameIdentity.id
```
- [Email](https://wasp.sh/docs/auth/email) data
```ts
const emailIdentity = user.identities.email
// Email address the user used to sign up, e.g. "fluffyllama@app.com".
emailIdentity.id
// `true` if the user has verified their email address.
emailIdentity.isEmailVerified
// Datetime when the email verification email was sent.
emailIdentity.emailVerificationSentAt
// Datetime when the last password reset email was sent.
emailIdentity.passwordResetSentAt
```
- [Google](https://wasp.sh/docs/auth/social-auth/google) data
```ts
const googleIdentity = user.identities.google
// Google User ID for example "123456789012345678901"
googleIdentity.id
```
- [GitHub](https://wasp.sh/docs/auth/social-auth/github) data
```ts
const githubIdentity = user.identities.github
// GitHub User ID for example "12345678"
githubIdentity.id
```
- [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak) data
```ts
const keycloakIdentity = user.identities.keycloak
// Keycloak User ID for example "12345678-1234-1234-1234-123456789012"
keycloakIdentity.id
```
- [Discord](https://wasp.sh/docs/auth/social-auth/discord) data
```ts
const discordIdentity = user.identities.discord
// Discord User ID for example "80351110224678912"
discordIdentity.id
```
If you support multiple auth methods, you'll need to find which identity exists for the user and then access its data:
```ts
if (user.identities.email !== null) {
const email = user.identities.email.id
// ...
} else if (user.identities.google !== null) {
const googleId = user.identities.google.id
// ...
}
```
#### `getFirstProviderUserId` Helper
The `getFirstProviderUserId` method returns the first user ID that it finds for the user. For example if the user has signed up with email, it will return the email. If the user has signed up with Google, it will return the Google ID.
This can be useful if you support multiple authentication methods and you need *any* ID that identifies the user in your app.
```tsx title="src/MainPage.tsx"
import { type AuthUser } from "wasp/auth"
const MainPage = ({ user }: { user: AuthUser }) => {
const userId = user.getFirstProviderUserId()
// ...
}
```
```ts title="src/tasks.ts"
export const createTask: CreateTask<...> = async (args, context) => {
const userId = context.user.getFirstProviderUserId()
// ...
}
```
\* Multiple identities per user will be possible in the future and then the `getFirstProviderUserId` method will return the ID of the first identity that it finds without any guarantees about which one it will be.
### Including the User with Other Entities
Sometimes, you might want to include the user's data when fetching other entities. For example, you might want to include the user's data with the tasks they have created.
We'll mention the `auth` and the `identities` relations which we will explain in more detail later in the [Entities Explained](#entities-explained) section.
:::caution[Be careful about sensitive data]
You'll need to include the `auth` and the `identities` relations to get the full auth data about the user. However, you should keep in mind that the `providerData` field in the `identities` can contain sensitive data like the user's hashed password (in case of email or username auth), so you will likely want to exclude it if you are returning those values to the client.
:::
You can include the full user's data with other entities using the `include` option in the Prisma queries:
```ts title="src/tasks.ts"
export const getAllTasks = (async (args, context) => {
return context.entities.Task.findMany({
orderBy: { id: "desc" },
select: {
id: true,
title: true,
user: {
include: {
auth: {
include: {
identities: {
// Including only the `providerName` and `providerUserId` fields
select: {
providerName: true,
providerUserId: true,
},
},
},
},
},
},
},
})
}) satisfies tasks.GetAllQuery<{}, {}>
```
If you have some **piece of the auth data that you want to access frequently** (for example the `username`), it's best to store it at the top level of the `User` entity.
For example, save the `username` or `email` as a property on the `User` and you'll be able to access it without including the `auth` and `identities` fields. We show an example in the [Defining Extra Fields on the User Entity](https://wasp.sh/docs/auth/overview#1-defining-extra-fields) section of the docs.
#### Getting Auth Data from the User Object
When you have the `user` object with the `auth` and `identities` fields, it can be a bit tedious to obtain the auth data (like username or Google ID) from it:
```tsx title="src/MainPage.tsx"
function MainPage() {
// ...
return (
{tasks.map((task) => (
{task.title} by {task.user.auth?.identities[0].providerUserId}
))}
)
}
```
Wasp offers a few helper methods to access the user's auth data when you retrieve the `user` like this. They are `getUsername`, `getEmail` and `getFirstProviderUserId`. They can be used both on the client and the server.
##### `getUsername`
It accepts the `user` object and if the user signed up with the [Username & password](https://wasp.sh/docs/auth/username-and-pass) auth method, it returns the username or `null` otherwise. The `user` object needs to have the `auth` and the `identities` relations included.
```tsx title="src/MainPage.tsx"
import { getUsername } from "wasp/auth"
function MainPage() {
// ...
return (
{tasks.map((task) => (
{task.title} by {getUsername(task.user)}
))}
)
}
```
##### `getEmail`
It accepts the `user` object and if the user signed up with the [Email](https://wasp.sh/docs/auth/email) auth method, it returns the email or `null` otherwise. The `user` object needs to have the `auth` and the `identities` relations included.
```tsx title="src/MainPage.tsx"
import { getEmail } from "wasp/auth"
function MainPage() {
// ...
return (
{tasks.map((task) => (
{task.title} by {getEmail(task.user)}
))}
)
}
```
##### `getFirstProviderUserId`
It returns the first user ID that it finds for the user. For example if the user has signed up with email, it will return the email. If the user has signed up with Google, it will return the Google ID. The `user` object needs to have the `auth` and the `identities` relations included.
```tsx title="src/MainPage.tsx"
import { getFirstProviderUserId } from "wasp/auth"
function MainPage() {
// ...
return (
{tasks.map((task) => (
{task.title} by {getFirstProviderUserId(task.user)}
))}
)
}
```
### Entities Explained
To store user's auth information, Wasp does a few things behind the scenes. Wasp takes your `schema.prisma` file and combines it with additional entities to create the final `schema.prisma` file that is used in your app.
In this section, we will explain which entities are created and how they are connected.
#### User Entity
When you want to add authentication to your app, you need to specify the `userEntity` field.
For example, you might set it to `User`:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
// ...
},
// ...
})
```
And define the `User` in the `schema.prisma` file:
```prisma title="schema.prisma"
model User {
id Int @id @default(autoincrement())
// Any other fields you want to store about the user
}
```
The `User` entity is a "business logic user" which represents a user of your app.
You can use this entity to store any information about the user that you want to store. For example, you might want to store the user's name or address.
You can also use the user entity to define the relations between users and other entities in your app. For example, you might want to define a relation between a user and the tasks that they have created.
You **own** the user entity and you can modify it as you wish. You can add new fields to it, remove fields from it, or change the type of the fields. You can also add new relations to it or remove existing relations from it.

Auth Entities in a Wasp App
On the other hand, the `Auth`, `AuthIdentity` and `Session` entities are created behind the scenes and are used to store the user's login credentials. You as the developer don't need to care about this entity most of the time. Wasp **owns** these entities.
In the case you want to create a custom signup action, you will need to use the `Auth` and `AuthIdentity` entities directly.
#### Example App Model
Let's imagine we created a simple tasks management app:
- The app has email and Google-based auth.
- Users can create tasks and see the tasks that they have created.
Let's look at how would that look in the database:

Example of Auth Entities
If we take a look at an example user in the database, we can see:
- The business logic user, `User` is connected to multiple `Task` entities.
- In this example, "Example User" has two tasks.
- The `User` is connected to exactly one `Auth` entity.
- Each `Auth` entity can have multiple `AuthIdentity` entities.
- In this example, the `Auth` entity has two `AuthIdentity` entities: one for the email-based auth and one for the Google-based auth.
- Each `Auth` entity can have multiple `Session` entities.
- In this example, the `Auth` entity has one `Session` entity.
:::caution[Using multiple auth identities for a single user]
Wasp currently doesn't support multiple auth identities for a single user. This means, for example, that a user can't have both an email-based auth identity and a Google-based auth identity. This is something we will add in the future with the introduction of the [account merging feature](https://github.com/wasp-lang/wasp/issues/954).
Account merging means that multiple auth identities can be merged into a single user account. For example, a user's email and Google identity can be merged into a single user account. Then the user can log in with either their email or Google account and they will be logged into the same account.
:::
#### `Auth` Entity internal {#auth-entity-}
Wasp's internal `Auth` entity is used to connect the business logic user, `User` with the user's login credentials.
```prisma
model Auth {
id String @id @default(uuid())
userId Int? @unique
// Wasp injects this relation on the User entity as well
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
}
```
The `Auth` fields:
- `id` is a unique identifier of the `Auth` entity.
- `userId` is a foreign key to the `User` entity.
- It is used to connect the `Auth` entity with the business logic user.
- `user` is a relation to the `User` entity.
- This relation is injected on the `User` entity as well.
- `identities` is a relation to the `AuthIdentity` entity.
- `sessions` is a relation to the `Session` entity.
#### `AuthIdentity` Entity internal {#authidentity-entity-}
The `AuthIdentity` entity is used to store the user's login credentials for various authentication methods.
```prisma
model AuthIdentity {
providerName String
providerUserId String
providerData String @default("{}")
authId String
auth Auth @relation(fields: [authId], references: [id], onDelete: Cascade)
@@id([providerName, providerUserId])
}
```
The `AuthIdentity` fields:
- `providerName` is the name of the authentication provider.
- For example, `email` or `google`.
- `providerUserId` is the user's ID in the authentication provider.
- For example, the user's email or Google ID.
- `providerData` is a JSON string that contains additional data about the user from the authentication provider.
- For example, for password based auth, this field contains the user's hashed password.
- This field is a `String` and not a `Json` type because [Prisma doesn't support the `Json` type for SQLite](https://github.com/prisma/prisma/issues/3786).
- `authId` is a foreign key to the `Auth` entity.
- It is used to connect the `AuthIdentity` entity with the `Auth` entity.
- `auth` is a relation to the `Auth` entity.
#### `Session` Entity internal {#session-entity-}
The `Session` entity is used to store the user's session information. It is used to keep the user logged in between page refreshes.
```prisma
model Session {
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)
@@index([userId])
}
```
The `Session` fields:
- `id` is a unique identifier of the `Session` entity.
- `expiresAt` is the date when the session expires.
- `userId` is a foreign key to the `Auth` entity.
- It is used to connect the `Session` entity with the `Auth` entity.
- `auth` is a relation to the `Auth` entity.
### Custom Signup Action
Let's take a look at how you can use the `Auth` and `AuthIdentity` entities to create custom login and signup actions. For example, you might want to create a custom signup action that creates a user in your app and also creates a user in a third-party service.
:::info[Custom Signup Examples]
In the Advanced section you can see an example for [Email](https://wasp.sh/docs/auth/advanced/custom-auth-actions#email) or [Username and password](https://wasp.sh/docs/auth/advanced/custom-auth-actions#username-and-password) authentication.
:::
Below is a simplified version of a custom signup action which you probably wouldn't use in your app but it shows you how you can use the `Auth` and `AuthIdentity` entities to create a custom signup action.
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { customSignup } from "./src/auth/signup" with { type: "ref" }
export default app({
// ...
spec: [
action(customSignup, { entities: ["User"] }),
],
})
```
```ts title="src/auth/signup.ts"
import {
createProviderId,
sanitizeAndSerializeProviderData,
createUser,
} from "wasp/server/auth"
import type { CustomSignup } from "wasp/server/operations"
type CustomSignupInput = {
username: string
password: string
}
type CustomSignupOutput = {
success: boolean
message: string
}
export const customSignup: CustomSignup<
CustomSignupInput,
CustomSignupOutput
> = async (args, { entities: { User } }) => {
try {
// Provider ID is a combination of the provider name and the provider user ID
// And it is used to uniquely identify the user in your app
const providerId = createProviderId("username", args.username)
// sanitizeAndSerializeProviderData hashes the password and returns a JSON string
const providerData = await sanitizeAndSerializeProviderData<"username">({
hashedPassword: args.password,
})
await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{}
)
// This is equivalent to:
// await User.create({
// data: {
// auth: {
// create: {
// identities: {
// create: {
// providerName: "username",
// providerUserId: args.username
// providerData,
// },
// },
// }
// },
// }
// })
} catch (e) {
return {
success: false,
message: e.message,
}
}
// Your custom code after sign-up.
// ...
return {
success: true,
message: "User created successfully",
}
}
```
You can use whichever method suits your needs better: either the `createUser` function or Prisma's `User.create` method. The `createUser` function is a bit more convenient to use because it hides some of the complexity. On the other hand, the `User.create` method gives you more control over the data that is stored in the `Auth` and `AuthIdentity` entities.
## Authentication / Auth Hooks
Auth hooks allow you to "hook into" the auth process at various stages and run your custom code. For example, if you want to forbid certain emails from signing up, or if you wish to send a welcome email to the user after they sign up, auth hooks are the way to go.
### Supported hooks
The following auth hooks are available in Wasp:
- [`onBeforeSignup`](#executing-code-before-the-user-signs-up)
- [`onAfterSignup`](#executing-code-after-the-user-signs-up)
- [`onAfterEmailVerified`](#executing-code-after-a-user-verifies-their-email)
- [`onBeforeOAuthRedirect`](#executing-code-before-the-oauth-redirect)
- [`onBeforeLogin`](#executing-code-before-the-user-logs-in)
- [`onAfterLogin`](#executing-code-after-the-user-logs-in)
We'll go through each of these hooks in detail. But first, let's see how the hooks fit into the auth flows:

Signup Flow with Hooks

Login Flow with Hooks \*
\* When using the OAuth auth providers, the login hooks are both called before the session is created but the session is created quickly afterward, so it shouldn't make any difference in practice.
Users registering with [email](https://wasp.sh/docs/auth/email) must verify it before they can log in. This verification triggers the Email verification flow:

Email Verification Flow with Hooks
Users signing in with [OAuth](https://wasp.sh/docs/auth/social-auth/overview) must authorize access before completing login. This authorization triggers the OAuth consent flow:

OAuth Flow with Hooks
### Using hooks
To use auth hooks, you must first declare them in the Wasp file:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import {
onBeforeSignup,
onAfterSignup,
onAfterEmailVerified,
onBeforeOAuthRedirect,
onBeforeLogin,
onAfterLogin,
} from "./src/auth/hooks" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
// ...
},
onBeforeSignup,
onAfterSignup,
onAfterEmailVerified,
onBeforeOAuthRedirect,
onBeforeLogin,
onAfterLogin,
},
// ...
})
```
If the hooks are defined as async functions, Wasp *awaits* them. This means the auth process waits for the hooks to finish before continuing.
Wasp ignores the hooks' return values. The only exception is the `onBeforeOAuthRedirect` hook, whose return value affects the OAuth redirect URL.
We'll now go through each of the available hooks.
#### Executing code before the user signs up
Wasp calls the `onBeforeSignup` hook before the user is created.
The `onBeforeSignup` hook can be useful if you want to reject a user based on some criteria before they sign up.
Works with [Email](https://wasp.sh/docs/auth/email) [Username & Password](https://wasp.sh/docs/auth/username-and-pass) [Slack](https://wasp.sh/docs/auth/social-auth/slack) [Discord](https://wasp.sh/docs/auth/social-auth/discord) [Github](https://wasp.sh/docs/auth/social-auth/github) [Google](https://wasp.sh/docs/auth/social-auth/google) [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onBeforeSignup } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onBeforeSignup,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import { HttpError } from "wasp/server"
import type { OnBeforeSignupHook } from "wasp/server/auth"
export const onBeforeSignup: OnBeforeSignupHook = async ({
providerId,
prisma,
req,
}) => {
const count = await prisma.user.count()
console.log("number of users before", count)
console.log("provider name", providerId.providerName)
console.log("provider user ID", providerId.providerUserId)
if (count > 100) {
throw new HttpError(403, "Too many users")
}
if (
providerId.providerName === "email" &&
providerId.providerUserId === "some@email.com"
) {
throw new HttpError(403, "This email is not allowed")
}
}
```
Read more about the data the `onBeforeSignup` hook receives in the [API Reference](#the-onbeforesignup-hook).
#### Executing code after the user signs up
Wasp calls the `onAfterSignup` hook after the user is created.
The `onAfterSignup` hook can be useful if you want to send the user a welcome email or perform some other action after the user signs up like syncing the user with a third-party service.
Since the `onAfterSignup` hook receives the OAuth tokens, you can use this hook to store the OAuth access token and/or [refresh token](#refreshing-the-oauth-access-token) in your database.
Works with [Email](https://wasp.sh/docs/auth/email) [Username & Password](https://wasp.sh/docs/auth/username-and-pass) [Slack](https://wasp.sh/docs/auth/social-auth/slack) [Discord](https://wasp.sh/docs/auth/social-auth/discord) [Github](https://wasp.sh/docs/auth/social-auth/github) [Google](https://wasp.sh/docs/auth/social-auth/google) [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onAfterSignup } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onAfterSignup,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import type { OnAfterSignupHook } from "wasp/server/auth"
export const onAfterSignup: OnAfterSignupHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
const count = await prisma.user.count()
console.log("number of users after", count)
console.log("user object", user)
// If this is an OAuth signup, you have access to the OAuth tokens and the uniqueRequestId
if (oauth) {
console.log("accessToken", oauth.tokens.accessToken)
console.log("uniqueRequestId", oauth.uniqueRequestId)
const id = oauth.uniqueRequestId
const data = someKindOfStore.get(id)
if (data) {
console.log("saved data for the ID", data)
}
someKindOfStore.delete(id)
}
}
```
Read more about the data the `onAfterSignup` hook receives in the [API Reference](#the-onaftersignup-hook).
#### Executing code after a user verifies their email
Wasp calls the `onAfterEmailVerified` hook exactly once, after the user verifies their email.
The `onAfterEmailVerified` hook is useful for triggering actions in response to the verification event โ such as sending a welcome email or syncing user data with a third-party service.
The `onAfterEmailVerified` hook receives an `email` string and `user` object, this makes it easy to perform personalized actions upon email verification.
Works with [Email](https://wasp.sh/docs/auth/email)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onAfterEmailVerified } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onAfterEmailVerified,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import type { OnAfterEmailVerifiedHook } from "wasp/server/auth"
import { emailSender } from "wasp/server/email"
export const onAfterEmailVerified: OnAfterEmailVerifiedHook = async ({
email,
}) => {
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: email,
subject: "Thank you for verifying your email!",
text: `Your email ${email} has been successfully verified!`,
})
// ...
}
```
Read more about the data the `onAfterEmailVerified` hook receives in the [API Reference](#the-onafteremailverified-hook).
#### Executing code before the OAuth redirect
Wasp calls the `onBeforeOAuthRedirect` hook after the OAuth redirect URL is generated but before redirecting the user. This hook can access the request object sent from the client at the start of the OAuth process.
The `onBeforeOAuthRedirect` hook can be useful if you want to save some data (e.g. request query parameters) that you can use later in the OAuth flow. You can use the `uniqueRequestId` parameter to reference this data later in the `onAfterSignup` or `onAfterLogin` hooks.
Works with [Discord](https://wasp.sh/docs/auth/social-auth/discord) [Github](https://wasp.sh/docs/auth/social-auth/github) [Google](https://wasp.sh/docs/auth/social-auth/google) [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onBeforeOAuthRedirect } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onBeforeOAuthRedirect,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import type { OnBeforeOAuthRedirectHook } from "wasp/server/auth"
export const onBeforeOAuthRedirect: OnBeforeOAuthRedirectHook = async ({
url,
oauth,
prisma,
req,
}) => {
console.log("query params before oAuth redirect", req.query)
// Saving query params for later use in onAfterSignup or onAfterLogin hooks
const id = oauth.uniqueRequestId
someKindOfStore.set(id, req.query)
return { url }
}
```
This hook's return value must be an object that looks like this: `{ url: URL }`. Wasp uses the URL to redirect the user to the OAuth provider.
Read more about the data the `onBeforeOAuthRedirect` hook receives in the [API Reference](#the-onbeforeoauthredirect-hook).
#### Executing code before the user logs in
Wasp calls the `onBeforeLogin` hook before the user is logged in.
The `onBeforeLogin` hook can be useful if you want to reject a user based on some criteria before they log in.
Works with [Email](https://wasp.sh/docs/auth/email) [Username & Password](https://wasp.sh/docs/auth/username-and-pass) [Slack](https://wasp.sh/docs/auth/social-auth/slack) [Discord](https://wasp.sh/docs/auth/social-auth/discord) [Github](https://wasp.sh/docs/auth/social-auth/github) [Google](https://wasp.sh/docs/auth/social-auth/google) [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onBeforeLogin } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onBeforeLogin,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import { HttpError } from "wasp/server"
import type { OnBeforeLoginHook } from "wasp/server/auth"
export const onBeforeLogin: OnBeforeLoginHook = async ({
providerId,
user,
prisma,
req,
}) => {
if (
providerId.providerName === "email" &&
providerId.providerUserId === "some@email.com"
) {
throw new HttpError(403, "You cannot log in with this email")
}
}
```
Read more about the data the `onBeforeLogin` hook receives in the [API Reference](#the-onbeforelogin-hook).
#### Executing code after the user logs in
Wasp calls the `onAfterLogin` hook after the user logs in.
The `onAfterLogin` hook can be useful if you want to perform some action after the user logs in, like syncing the user with a third-party service.
Since the `onAfterLogin` hook receives the OAuth tokens, you can use it to update the OAuth access token for the user in your database. You can also use it to [refresh the OAuth access token](#refreshing-the-oauth-access-token) if the provider supports it.
Works with [Email](https://wasp.sh/docs/auth/email) [Username & Password](https://wasp.sh/docs/auth/username-and-pass) [Discord](https://wasp.sh/docs/auth/social-auth/discord) [Github](https://wasp.sh/docs/auth/social-auth/github) [Google](https://wasp.sh/docs/auth/social-auth/google) [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak)
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { onAfterLogin } from "./src/auth/hooks" with { type: "ref" }
export default app({
// ...
auth: {
// ...
onAfterLogin,
},
// ...
})
```
```ts title="src/auth/hooks.ts"
import type { OnAfterLoginHook } from "wasp/server/auth"
export const onAfterLogin: OnAfterLoginHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
console.log("user object", user)
// If this is an OAuth signup, you have access to the OAuth tokens and the uniqueRequestId
if (oauth) {
console.log("accessToken", oauth.tokens.accessToken)
console.log("uniqueRequestId", oauth.uniqueRequestId)
const id = oauth.uniqueRequestId
const data = someKindOfStore.get(id)
if (data) {
console.log("saved data for the ID", data)
}
someKindOfStore.delete(id)
}
}
```
Read more about the data the `onAfterLogin` hook receives in the [API Reference](#the-onafterlogin-hook).
#### Refreshing the OAuth access token
Some OAuth providers support refreshing the access token when it expires. To refresh the access token, you need the OAuth **refresh token**.
Wasp exposes the OAuth refresh token in the `onAfterSignup` and `onAfterLogin` hooks. You can store the refresh token in your database and use it to refresh the access token when it expires.
Import the provider object with the OAuth client from the `wasp/server/auth` module. For example, to refresh the Google OAuth access token, import the `google` object from the `wasp/server/auth` module. You use the `refreshAccessToken` method of the OAuth client to refresh the access token.
Here's an example of how you can refresh the access token for Google OAuth:
```ts title="src/auth/hooks.ts"
import type { OnAfterLoginHook } from "wasp/server/auth"
import { google } from "wasp/server/auth"
export const onAfterLogin: OnAfterLoginHook = async ({ oauth }) => {
if (oauth.provider === "google" && oauth.tokens.refreshToken !== null) {
const newTokens = await google.oAuthClient.refreshAccessToken(
oauth.tokens.refreshToken
)
log("new tokens", newTokens)
}
}
```
Google exposes the `accessTokenExpiresAt` field in the `oauth.tokens` object. You can use this field to determine when the access token expires.
If you want to refresh the token periodically, use a [Wasp Job](https://wasp.sh/docs/advanced/jobs).
### API Reference
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import {
onBeforeSignup,
onAfterSignup,
onAfterEmailVerified,
onBeforeOAuthRedirect,
onBeforeLogin,
onAfterLogin,
} from "./src/auth/hooks" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
userEntity: "User",
methods: {
// ...
},
onBeforeSignup,
onAfterSignup,
onAfterEmailVerified,
onBeforeOAuthRedirect,
onBeforeLogin,
onAfterLogin,
},
// ...
})
```
#### Common hook input
The following properties are available in all auth hooks:
- `prisma: PrismaClient`
The Prisma client instance which you can use to query your database.
- `req: Request`
The [Express request object](https://expressjs.com/en/api.html#req) from which you can access the request headers, cookies, etc.
#### The `onBeforeSignup` hook
```ts title="src/auth/hooks.ts"
import type { OnBeforeSignupHook } from "wasp/server/auth"
export const onBeforeSignup: OnBeforeSignupHook = async ({
providerId,
prisma,
req,
}) => {
// Hook code goes here
}
```
The hook receives an object as **input** with the following properties:
- [`providerId: ProviderId`](#providerid-fields)
- Plus the [common hook input](#common-hook-input)
Wasp ignores this hook's **return value**.
#### The `onAfterSignup` hook
```ts title="src/auth/hooks.ts"
import type { OnAfterSignupHook } from "wasp/server/auth"
export const onAfterSignup: OnAfterSignupHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
// Hook code goes here
}
```
The hook receives an object as **input** with the following properties:
- [`providerId: ProviderId`](#providerid-fields)
- `user: User`
The user object that was created.
- [`oauth?: OAuthFields`](#oauth-fields)
- Plus the [common hook input](#common-hook-input)
Wasp ignores this hook's **return value**.
#### The `onAfterEmailVerified` hook
```ts title="src/auth/hooks.ts"
import type { OnAfterEmailVerifiedHook } from "wasp/server/auth"
export const onAfterEmailVerified: OnAfterEmailVerifiedHook = async ({
email,
user,
prisma,
req,
}) => {
// Hook code goes here
}
```
The hook receives an object as **input** with the following properties:
- `email: string`
The user's veriried email address.
- `user: User`
The user who completed email verification.
- Plus the [common hook input](#common-hook-input)
Wasp ignores this hook's **return value**.
#### The `onBeforeOAuthRedirect` hook
```ts title="src/auth/hooks.ts"
import type { OnBeforeOAuthRedirectHook } from "wasp/server/auth"
export const onBeforeOAuthRedirect: OnBeforeOAuthRedirectHook = async ({
url,
oauth,
prisma,
req,
}) => {
// Hook code goes here
return { url }
}
```
The hook receives an object as **input** with the following properties:
- `url: URL`
Wasp uses the URL for the OAuth redirect.
- `oauth: { uniqueRequestId: string }`
The `oauth` object has the following fields:
- `uniqueRequestId: string`
The unique request ID for the OAuth flow (you might know it as the `state` parameter in OAuth.)
You can use the unique request ID to save data (e.g. request query params) that you can later use in the `onAfterSignup` or `onAfterLogin` hooks.
- Plus the [common hook input](#common-hook-input)
This hook's return value must be an object that looks like this: `{ url: URL }`. Wasp uses the URL to redirect the user to the OAuth provider.
#### The `onBeforeLogin` hook
```ts title="src/auth/hooks.ts"
import type { OnBeforeLoginHook } from "wasp/server/auth"
export const onBeforeLogin: OnBeforeLoginHook = async ({
providerId,
prisma,
req,
}) => {
// Hook code goes here
}
```
The hook receives an object as **input** with the following properties:
- [`providerId: ProviderId`](#providerid-fields)
- `user: User`
The user that is trying to log in.
- Plus the [common hook input](#common-hook-input)
Wasp ignores this hook's **return value**.
#### The `onAfterLogin` hook
```ts title="src/auth/hooks.ts"
import type { OnAfterLoginHook } from "wasp/server/auth"
export const onAfterLogin: OnAfterLoginHook = async ({
providerId,
user,
oauth,
prisma,
req,
}) => {
// Hook code goes here
}
```
The hook receives an object as **input** with the following properties:
- [`providerId: ProviderId`](#providerid-fields)
- `user: User`
The logged-in user's object.
- [`oauth?: OAuthFields`](#oauth-fields)
- Plus the [common hook input](#common-hook-input)
Wasp ignores this hook's **return value**.
#### ProviderId fields
The `providerId` object represents the user for the current authentication method. Wasp passes it to the `onBeforeSignup`, `onAfterSignup`, `onBeforeLogin`, and `onAfterLogin` hooks.
It has the following fields:
- `providerName: string`
The provider's name (e.g. `"email"`, `"google"`, `"github"`)
- `providerUserId: string`
The user's unique ID in the provider's system (e.g. email, Google ID, GitHub ID)
#### OAuth fields
Wasp passes the `oauth` object to the `onAfterSignup` and `onAfterLogin` hooks only when the user is authenticated with [Social Auth](https://wasp.sh/docs/auth/social-auth/overview).
It has the following fields:
- `providerName: string`
The name of the OAuth provider the user authenticated with (e.g. `"google"`, `"github"`).
- `tokens: Tokens`
You can use the OAuth tokens to make requests to the provider's API on the user's behalf.
Depending on the OAuth provider, the `tokens` object might have different fields. For example, Google has the fields `accessToken`, `refreshToken`, `idToken`, and `accessTokenExpiresAt`.
To access the provider-specific fields, you must first narrow down the `oauth.tokens` object type to the specific OAuth provider type.
```ts
if (oauth && oauth.providerName === "google") {
console.log(oauth.tokens.accessToken)
// ^ Google specific tokens are available here
console.log(oauth.tokens.refreshToken)
console.log(oauth.tokens.idToken)
console.log(oauth.tokens.accessTokenExpiresAt)
}
```
- `uniqueRequestId: string`
The unique request ID for the OAuth flow (you might know it as the `state` parameter in OAuth.)
You can use the unique request ID to get the data that was saved in the `onBeforeOAuthRedirect` hook.
## Authentication / Advanced / Custom sign-up actions
If you need to deeply hook into the sign-up process, you can create your own sign-up action and customize the code to, for example, add extra validation, store more data, or otherwise call custom code at registration time.
:::danger
Custom sign-up actions are complex, and we don't recommend creating a custom sign-up action unless you have a good reason to do so. They also require you to be careful, as any small mistake will compromise the security of your app.
Before using custom actions, check if our support for [custom auth UI](https://wasp.sh/docs/auth/overview#custom-auth-ui) and for [auth hooks](https://wasp.sh/docs/auth/auth-hooks) could fit well with you requirements.
:::
You are not able to use Wasp UI with custom sign-up actions, so you're expected to implemented your own UI and call the custom actions you create from it.
### Example code
Below you will find a starting point for creating your own actions. The given implementation is similar to what Wasp does under the hood, and it is up to you to customize it.
#### Email
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { onBeforeSignup } from "./src/auth/hooks" with { type: "ref" }
import { customSignup } from "./src/auth/signup" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [""],
auth: {
// ...
onBeforeSignup,
},
spec: [
action(customSignup),
],
})
```
```ts title="src/auth/hooks.ts"
import { HttpError } from "wasp/server";
// This disables Wasp's default sign-up action
export const onBeforeSignup = async () => {
throw new HttpError(403, "This sign-up method is disabled");
};
```
```ts title="src/auth/signup.ts"
import type { CustomSignup } from "wasp/server/operations";
import { HttpError } from "wasp/server";
import {
createEmailVerificationLink,
createProviderId,
createUser,
ensurePasswordIsPresent,
ensureValidEmail,
ensureValidPassword,
findAuthIdentity,
getProviderData,
sanitizeAndSerializeProviderData,
sendEmailVerificationEmail,
} from "wasp/server/auth";
type CustomSignupInput = {
email: string;
password: string;
};
type CustomSignupOutput = {
success: boolean;
message: string;
};
export const customSignup: CustomSignup<
CustomSignupInput,
CustomSignupOutput
> = async (args, _context) => {
ensureValidEmail(args);
ensurePasswordIsPresent(args);
ensureValidPassword(args);
try {
const providerId = createProviderId("email", args.email);
const existingAuthIdentity = await findAuthIdentity(providerId);
let providerData;
if (existingAuthIdentity) {
// User already exists, handle accordingly
// For example, throw an error or return a message
throw new HttpError(400, "Email already exists.");
// Or, another example, you can check if the user is already
// verified and re-send the verification email if not
providerData = getProviderData<"email">(
existingAuthIdentity.providerData,
);
if (providerData.isEmailVerified)
throw new HttpError(400, "Email already verified.");
}
if (!providerData) {
providerData = await sanitizeAndSerializeProviderData<"email">({
// The provider will hash the password for us, so we don't need to do it here.
hashedPassword: args.password,
isEmailVerified: false,
emailVerificationSentAt: null,
passwordResetSentAt: null,
});
await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{},
);
}
// Verification link links to a client route e.g. /email-verification
const verificationLink = await createEmailVerificationLink(
args.email,
"/email-verification",
);
try {
await sendEmailVerificationEmail(args.email, {
from: {
name: "My App Postman",
email: "hello@itsme.com",
},
to: args.email,
subject: "Verify your email",
text: `Click the link below to verify your email: ${verificationLink}`,
html: `
Click the link below to verify your email
Verify email
`,
});
} catch (e: unknown) {
console.error("Failed to send email verification email:", e);
throw new HttpError(500, "Failed to send email verification email.");
}
} catch (e: any) {
return {
success: false,
message: e.message,
};
}
// Your custom code after sign-up.
// ...
return {
success: true,
message: "User created successfully",
};
};
```
#### Username and password
```ts title="main.wasp.ts"
import { action, app } from "@wasp.sh/spec"
import { onBeforeSignup } from "./src/auth/hooks" with { type: "ref" }
import { customSignup } from "./src/auth/signup" with { type: "ref" }
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
auth: {
// ...
onBeforeSignup,
},
spec: [
action(customSignup),
],
})
```
```ts title="src/auth/hooks.ts"
import { HttpError } from "wasp/server";
// This disables Wasp's default sign-up action
export const onBeforeSignup = async () => {
throw new HttpError(403, "This sign-up method is disabled");
};
```
```ts title="src/auth/signup.ts"
import type { CustomSignup } from "wasp/server/operations";
import {
createProviderId,
createUser,
ensurePasswordIsPresent,
ensureValidPassword,
ensureValidUsername,
sanitizeAndSerializeProviderData,
} from "wasp/server/auth";
type CustomSignupInput = {
username: string;
password: string;
};
type CustomSignupOutput = {
success: boolean;
message: string;
};
export const customSignup: CustomSignup<
CustomSignupInput,
CustomSignupOutput
> = async (args, _context) => {
ensureValidUsername(args);
ensurePasswordIsPresent(args);
ensureValidPassword(args);
try {
const providerId = createProviderId("username", args.username);
const providerData = await sanitizeAndSerializeProviderData<"username">({
// The provider will hash the password for us, so we don't need to do it here.
hashedPassword: args.password,
});
await createUser(providerId, providerData, {});
} catch (e: any) {
console.error("Error creating user:", e);
return {
success: false,
message: e.message,
};
}
return {
success: true,
message: "User created successfully",
};
};
```
### Validators API Reference
We suggest using the built-in field validators for your authentication flow. You can import them from `wasp/server/auth`. These are the same validators that Wasp uses internally for the default authentication flow.
##### Username
- `ensureValidUsername(args)`
Checks if the username is valid and throws an error if it's not. Read more about the validation rules [here](https://wasp.sh/docs/auth/overview#default-validations).
##### Email
- `ensureValidEmail(args)`
Checks if the email is valid and throws an error if it's not. Read more about the validation rules [here](https://wasp.sh/docs/auth/overview#default-validations).
##### Password
- `ensurePasswordIsPresent(args)`
Checks if the password is present and throws an error if it's not.
- `ensureValidPassword(args)`
Checks if the password is valid and throws an error if it's not. Read more about the validation rules [here](https://wasp.sh/docs/auth/overview#default-validations).
## Project Setup / Starter Templates
We created a few starter templates to help you get started with Wasp. Check out the list [below](#available-templates).
### Using a Template
Run `wasp new` to run the interactive mode for creating a new Wasp project.
It will ask you for the project name, and then for the template to use:
```
$ wasp new
Enter the project name (e.g. my-project) โธ MyFirstProject
Choose a starter template
[1] basic (default)
A basic starter template designed to help you get up and running quickly.
It features examples covering the most common use cases.
[2] minimal
A minimal starter template that features just a single page.
[3] saas
Everything a SaaS needs! Comes with Auth, ChatGPT API, Tailwind, Stripe payments and more.
Check out https://opensaas.sh/ for more details.
โธ 1
๐ --- Creating your project from the "basic" template... -------------------------
Created new Wasp app in ./MyFirstProject directory!
To run your new app, do:
cd MyFirstProject
wasp db migrate-dev
wasp start
```
### Available Templates
When you have a good idea for a new product, you don't want to waste your time on setting up common things like authentication, database, etc. That's why we created a few starter templates to help you get started with Wasp.
#### OpenSaaS.sh template

Everything a SaaS needs! Comes with Auth, ChatGPT API, Tailwind, Stripe payments and more. Check out for more details.
**Features:** Stripe Payments, OpenAI GPT API, Google Auth, SendGrid, Tailwind, & Cron Jobs
Use this template:
```
wasp new -t saas
```
#### Minimal Template
A minimal starter template that features just a single page. Perfect for starting from scratch with the bare essentials.
Use this template:
```
wasp new -t minimal
```
## Project Setup / Customizing the App
Each Wasp project can have only one `app` spec. It is used to configure your app and its components.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "todoApp",
wasp: { version: "^0.25" },
title: "ToDo App",
head: [
"",
"",
],
// ...
})
```
We'll go through some common customizations you might want to do to your app. For more details on each of the fields, check out the [API Reference](#api-reference).
#### Changing the App Title
You may want to change the title of your app, which appears in the browser tab, next to the favicon. You can change it by changing the `title` field of your `app` spec:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "BookFace",
// ...
})
```
#### Adding Additional Lines to the Head
If you are looking to add additional style sheets or scripts to your app, you can do so by adding them to the `head` field of your `app` spec.
An example of adding extra style sheets and scripts:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
wasp: { version: "^0.25" },
title: "My App",
head: [ // optional
"",
"",
"",
"",
],
// ...
})
```
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/App)
#### [App ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/App)
[All the options for the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/App)
## Project Setup / Client Config
You can configure the client using the `client` field inside the `app` spec:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import Root from "./src/Root" with { type: "ref" }
import mySetupFunction from "./src/myClientSetupCode" with { type: "ref" }
export default app({
name: "MyApp",
client: {
rootComponent: Root,
setupFn: mySetupFunction,
},
// ...
})
```
### Root Component
Wasp gives you the option to define a "wrapper" component for your React app.
It can be used for a variety of purposes, but the most common ones are:
- Defining a common layout for your application.
- Setting up various providers that your application needs.
#### Defining a Common Layout
Let's define a common layout for your application:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import Root from "./src/Root" with { type: "ref" }
export default app({
name: "MyApp",
client: {
rootComponent: Root,
},
// ...
})
```
```tsx title="src/Root.tsx"
import { Outlet } from "react-router"
export default function Root() {
return (
My App
)
}
```
You need to import the [`Outlet`](https://reactrouter.com/8.0.1/api/components/Outlet) component from `react-router` and put it where you want the current page to be rendered.
#### Setting up a Provider
This is how to set up various providers that your application needs:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import Root from "./src/Root" with { type: "ref" }
export default app({
name: "MyApp",
client: {
rootComponent: Root,
},
// ...
})
```
```tsx title="src/Root.tsx"
import { Outlet } from "react-router"
import store from "./store"
import { Provider } from "react-redux"
export default function Root() {
return (
)
}
```
As long as you render the `Outlet` component, you can put what ever you want in the root component.
For the full description of the `rootComponent` field, check the [`Client` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Client#rootcomponent).
### Setup Function
`setupFn` declares a Typescript function that Wasp executes on the client before everything else.
:::caution[The setup function can also run on the server]
The setup function can also run during server-side rendering, like when [prerendering](https://wasp.sh/docs/advanced/prerendering) pages, where browser APIs like `window` are not available. See [Running Code Only on the Client](#running-code-only-on-the-client).
:::
#### Running Some Code
We can run any code we want in the setup function.
For example, here's a setup function that logs a message every hour:
```ts title="src/myClientSetupCode.ts"
export default async function mySetupFunction(): Promise {
let count = 1
setInterval(
() => console.log(`You have been online for ${count++} hours.`),
1000 * 60 * 60
)
}
```
#### Running Code Only on the Client
If your app uses [prerendering](https://wasp.sh/docs/advanced/prerendering), Wasp also executes the setup function while rendering your pages on the server. There, browser APIs like `window`, `document`, or `localStorage` don't exist, so using them would crash the prerender. Side effects like timers or event listeners would also run in the Node.js process.
To run some code only in the browser, check Vite's [`import.meta.env.SSR`](https://vite.dev/guide/env-and-mode.html#env-variables) flag, which is `true` during server-side rendering and `false` on the client:
```ts title="src/myClientSetupCode.ts"
export default async function mySetupFunction(): Promise {
if (import.meta.env.SSR) {
// We're rendering on the server, skip the browser-only setup.
return;
}
window.addEventListener("online", () => console.log("You are back online!"));
}
```
#### Overriding Default Behaviour for Queries
:::info
You can change the options for a **single** Query using the `options` object, as described [here](https://wasp.sh/docs/data-model/operations/queries#the-usequery-hook-1).
:::
Wasp's `useQuery` hook uses `react-query`'s `useQuery` hook under the hood. Since `react-query` comes configured with aggressive but sane default options, you most likely won't have to change those defaults for all Queries.
If you do need to change the global defaults, you can do so inside the client setup function.
Wasp exposes a `configureQueryClient` hook that lets you configure *react-query*'s `QueryClient` object:
```ts title="src/myClientSetupCode.ts"
import { configureQueryClient } from "wasp/client/operations"
export default async function mySetupFunction(): Promise {
// ... some setup
configureQueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
},
},
})
// ... some more setup
}
```
Make sure to pass in an object expected by the `QueryClient`'s constructor, as explained in [react-query's docs](https://tanstack.com/query/v4/docs/reference/QueryClient).
For the full description of the `setupFn` field, check the [`Client` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Client#setupfn).
### Base Directory
If you need to serve the client from a subdirectory, you can use the `baseDir` option:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "MyApp",
client: {
baseDir: "/my-app",
},
// ...
})
```
This means that if you serve your app from `https://example.com/my-app`, the router will work correctly, and all the assets will be served from `https://example.com/my-app`.
:::caution[Setting the correct env variable]
If you set the `baseDir` option, make sure that the `WASP_WEB_CLIENT_URL` env variable also includes that base directory.
For example, if you are serving your app from `https://example.com/my-app`, the `WASP_WEB_CLIENT_URL` should be also set to `https://example.com/my-app`, and not just `https://example.com`.
:::
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Client)
#### [Client ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Client)
[All the options for the client field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Client)
## Project Setup / Server Config
You can configure the behavior of the server via the `server` field of `app` spec:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { myMiddlewareConfigFn, mySetupFunction } from "./src/myServerSetupCode" with { type: "ref" }
export default app({
name: "MyApp",
server: {
setupFn: mySetupFunction,
middlewareConfigFn: myMiddlewareConfigFn,
},
// ...
})
```
### Setup Function
`setupFn` declares a Typescript function that will be executed on server start.
#### Adding a Custom Route
As an example, adding a custom route would look something like:
```ts title="src/myServerSetupCode.ts"
import { ServerSetupFn } from "wasp/server"
import { Application } from "express"
export const mySetupFunction: ServerSetupFn = async ({ app }) => {
addCustomRoute(app)
}
function addCustomRoute(app: Application) {
app.get("/customRoute", (_req, res) => {
res.send("I am a custom route")
})
}
```
#### Storing Some Values for Later Use
In case you want to store some values for later use, or to be accessed by the [Operations](https://wasp.sh/docs/data-model/operations/overview) you do that in the `setupFn` function.
Dummy example of such function and its usage:
```ts title="src/myServerSetupCode.ts"
import { type ServerSetupFn } from "wasp/server"
let someResource = undefined
export const mySetupFunction: ServerSetupFn = async () => {
// Let's pretend functions setUpSomeResource and startSomeCronJob
// are implemented below or imported from another file.
someResource = await setUpSomeResource()
startSomeCronJob()
}
export const getSomeResource = () => someResource
```
```ts title="src/queries.ts"
import { type SomeQuery } from "wasp/server/operations"
import { getSomeResource } from "./myServerSetupCode.js"
...
export const someQuery: SomeQuery<...> = async (args, context) => {
const someResource = getSomeResource()
return queryDataFromSomeResource(args, someResource)
}
```
:::note
The recommended way is to put the variable in the same module where you defined the setup function and then expose additional functions for reading those values, which you can then import directly from Operations and use.
This effectively turns your module into a singleton whose construction is performed on server start.
:::
For the full description of the `setupFn` field, check the [`Server` API Reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Server#setupfn).
### Middleware Config Function
You can configure the global middleware via the `middlewareConfigFn`. This will modify the middleware stack for all operations and APIs.
Read more in the [configuring middleware section](https://wasp.sh/docs/advanced/middleware-config#1-customize-global-middleware).
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Server)
#### [Server ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Server)
[All the options for the server field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/Server)
## Project Setup / Static Asset Handling
### Importing an Asset as URL
Importing a static asset (e.g. an image) will return its URL. For example:
```jsx title="src/App.tsx"
import imgUrl from './img.png'
function App() {
return
}
```
For example, `imgUrl` will be `/img.png` during development, and become `/assets/img.2d8efhg.png` in the production build.
This is what you want to use most of the time, as it ensures that the asset file exists and is included in the bundle.
We are using Vite under the hood, read more about importing static assets in Vite's [docs](https://vitejs.dev/guide/assets.html#importing-asset-as-url).
### The `public` Directory
If you have assets that are:
- Never referenced in source code (e.g. robots.txt)
- Must retain the exact same file name (without hashing)
- ...or you simply don't want to have to import an asset first just to get its URL
Then you can place the asset in the `public` directory at the root of your project:
```
.
โโโ public
โโโ favicon.ico
โโโ robots.txt
```
Assets in this directory will be served at root path `/` during development and copied to the root of the dist directory as-is.
For example, if you have a file `favicon.ico` in the `public` directory, and your app is hosted at `https://myapp.com`, it will be made available at `https://myapp.com/favicon.ico`.
:::info[Usage in client code]
Note that:
- You should always reference public assets using root absolute path
- for example, `public/icon.png` should be referenced in source code as `/icon.png`.
- Assets in the `public` directory **cannot be imported** from TypeScript.
:::
## Project Setup / Env Variables
**Environment variables** are used to configure projects based on the context in which they run. This allows them to exhibit different behaviors in different environments, such as development, staging, or production.
For instance, *during development*, you may want your project to connect to a local development database running on your machine, but *in production*, you want it to connect to the production database. Similarly, in development, you may want to use a test Stripe account, while in production, your app should use a real Stripe account.
While some env vars are required by Wasp, such as the database connection or secrets for social auth, you can also define your env vars for any other useful purposes, and then access them in the code.
Let's go over the available env vars in Wasp, how to define them, and how to use them in your project.
### Client Env Vars
Client environment variables are injected into the client Javascript code during the build process, making them public and readable by anyone. Therefore, you should **never store secrets in them** (such as secret API keys, you should store secrets in the server env variables).
:::caution[Client Env Var Prefix]
Client env vars must be prefixed with `REACT_APP_`, for example: `REACT_APP_SOME_VAR_NAME=...` for security reasons. Wasp will only inject env vars that start with this prefix into the client code to prevent accidental exposure of sensitive information.
:::
You can read them from the client code like this:
```ts title="src/App.ts"
import { env } from "wasp/client"
console.log(env.REACT_APP_SOME_VAR_NAME)
```
Read more about the `env` object in the [API reference](#client-env-vars-api).
#### Wasp Client Env Vars
Here are the client env vars that Wasp defines:
##### General Configuration {#client-general-configuration}
These are some general env variables used for various Wasp features:
| Name | Type | Notes |
| ------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `REACT_APP_API_URL` | URLrequired | The client uses this as the server URL. This app is required in production but defaults to `http://localhost:3001` in development. |
### Server Env Vars
You can store secret values (e.g. secret API keys) in the server env variables since they are not publicly readable. You can define them without any special prefix, such as `SOME_VAR_NAME=...`.
You can read the env vars from server code like this:
```ts
import { env } from "wasp/server"
console.log(env.SOME_VAR_NAME)
```
Read more about the `env` object in the [API reference](#server-env-vars-1).
#### Wasp Server Env Vars
##### General Configuration {#server-general-configuration}
These are some general env variables used for various Wasp features:
| Name | Type | Notes |
| --------------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `DATABASE_URL` | Stringrequired | The URL of the PostgreSQL database you want your app to use. |
| `WASP_WEB_CLIENT_URL` | URLrequired | Server uses this value as your client URL in various features e.g. linking to your app in e-mails. Defaults to `http://localhost:3000` in development. |
| `WASP_SERVER_URL` | URLrequired | Server uses this value as your server URL in various features e.g. to redirect users when logging in with OAuth providers like Google or GitHub. Defaults to `http://localhost:3001` in development. |
| `JWT_SECRET` | Stringrequired | A random string of at least 32 characters. Needed to generate secure tokens. Defaults to `DEVJWTSECRET` in development. Generate secret |
| `PORT` | IntegeroptionalDefault: `3001` | This is where the server listens for requests. |
##### SMTP Email Sender
If you are using `SMTP` as your email sender, you need to provide the following environment variables:
| Name | Type | Notes |
| --------------- | --------------- | ------------------------- |
| `SMTP_HOST` | Stringrequired | The SMTP server host. |
| `SMTP_PORT` | Integerrequired | The SMTP server port. |
| `SMTP_USERNAME` | Stringrequired | The SMTP server username. |
| `SMTP_PASSWORD` | Stringrequired | The SMTP server password. |
##### SendGrid Email Sender
If you are using `SendGrid` as your email sender, you need to provide the following environment variables:
| Name | Type | Notes |
| ------------------ | -------------- | --------------------- |
| `SENDGRID_API_KEY` | Stringrequired | The SendGrid API key. |
##### Mailgun Email Sender
If you are using `Mailgun` as your email sender, you need to provide the following environment variables:
| Name | Type | Notes |
| ----------------- | -------------- | ----------------------------------------------------------------------------- |
| `MAILGUN_API_KEY` | Stringrequired | The Mailgun API key. |
| `MAILGUN_DOMAIN` | Stringrequired | The Mailgun domain. |
| `MAILGUN_API_URL` | URLoptional | Useful if you want to use the EU API endpoint (`https://api.eu.mailgun.net`). |
##### Resend Email Sender
If you are using `Resend` as your email sender, you need to provide the following environment variables:
| Name | Type | Notes |
| ---------------- | -------------- | ------------------- |
| `RESEND_API_KEY` | Stringrequired | The Resend API key. |
##### OAuth Providers
If you are using OAuth, you need to provide the following environment variables:
| Name | Type | Notes |
| ------------------------------- | -------------- | ------------------------------------------------- |
| `_CLIENT_ID` | Stringrequired | The client ID provided by the OAuth provider. |
| `_CLIENT_SECRET` | Stringrequired | The client secret provided by the OAuth provider. |
\* `` is the uppercase name of the provider you are using. For example, if you are using Google OAuth, you need to provide the `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` environment variables.
If you are using [Keycloak](https://wasp.sh/docs/auth/social-auth/keycloak), you'll need to provide one extra environment variable:
| Name | Type | Notes |
| -------------------- | ----------- | ------------------------------ |
| `KEYCLOAK_REALM_URL` | URLrequired | The URL of the Keycloak realm. |
##### Jobs
| Name | Type | Notes |
| --------------------- | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `PG_BOSS_NEW_OPTIONS` | Stringoptional | A [JSON env var](#json-env-vars). Enables you to provide [custom config](https://wasp.sh/docs/advanced/jobs#pg_boss_new_options) for PgBoss. |
##### Development
We provide some helper env variables in development:
| Name | Type | Notes |
| -------------------------------- | ------------------------------- | -------------------------------------------------------------------------- |
| `SKIP_EMAIL_VERIFICATION_IN_DEV` | BooleanoptionalDefault: `false` | If set to true, automatically sets user emails as verified in development. |
### Defining Env Vars in Development
During development (`wasp start`), there are two ways to provide env vars to your Wasp project:
1. Using `.env` files. **(recommended)**
2. Using shell. (useful for overrides)
#### 1. Using .env (dotenv) Files {#dotenv-files}

This is the recommended method for providing env vars to your Wasp project during development.
In the root of your Wasp project you can create two distinct files:
- `.env.server` for env vars that will be provided to the server.
Variables are defined in these files in the form of `NAME=VALUE`, for example:
```shell title=".env.server"
DATABASE_URL=postgresql://localhost:5432
SOME_VAR_NAME=somevalue
```
- `.env.client` for env vars that will be provided to the client.
Variables are defined in these files in the form of `NAME=VALUE`, for example:
```shell title=".env.client"
REACT_APP_SOME_VAR_NAME=somevalue
```
:::caution[Client Env Var Prefix]
Client env vars must be prefixed with `REACT_APP_`, for example: `REACT_APP_SOME_VAR_NAME=...` for security reasons. Wasp will only inject env vars that start with this prefix into the client code to prevent accidental exposure of sensitive information.
:::
`.env.server` should not be committed to version control as it can contain secrets, while `.env.client` can be versioned as it must not contain any secrets. By default, in the `.gitignore` file that comes with a new Wasp app, we ignore all dotenv files.
#### 2. Using Shell
If you set environment variables in the shell where you run your Wasp commands (e.g., `wasp start`), Wasp will recognize them.
You can set environment variables in the `.profile` or a similar file, which will set them permanently, or you can set them temporarily by defining them at the start of a command (`SOME_VAR_NAME=SOMEVALUE wasp start`).
This is not specific to Wasp and is simply how environment variables can be set in the shell.
Defining environment variables in this way can be cumbersome even for a single project and even more challenging to manage if you have multiple Wasp projects. Therefore, we do not recommend this as a default method for providing environment variables to Wasp projects during development, you should use .env files instead. However, it can be useful for occasionally **overriding** specific environment variables because environment variables set this way **take precedence over those defined in `.env` files**.
### Defining Env Vars in Production
Defining env variables in production will depend on where you are deploying your Wasp project. In general, you will define them via mechanisms that your hosting provider provides.
We talk about how to define env vars for each deployment option in the [deployment section](https://wasp.sh/docs/deployment/env-vars).
### JSON Env Vars
Some of the environment variables you pass to Wasp are parsed as JSON values. This is useful for features needing more in-depth configuration, but it comes with the caveat of ensuring that the JSON syntax is valid.
The main issue comes in the form of escaping quotes, and the different ways to do it depending on where you are defining the env var.
##### In `.env` files
In `.env` files, you don't need to quote the full value, so you don't need to escape the quotes. For example, you can define a JSON object like this:
```shell title=".env.server"
PG_BOSS_NEW_OPTIONS={"connectionString":"...db url...","jobExpirationInSeconds":60,"maxRetries":3}
```
##### In the shell
In the shell, you need to quote the full value and escape the quotes inside the JSON object. For example, you can define a JSON object like this:
```shell
PG_BOSS_NEW_OPTIONS="{\"connectionString\":\"...db url...\",\"jobExpirationInSeconds\":60,\"maxRetries\":3}"
```
As an alternative, you can use single quotes to avoid escaping the quotes inside the JSON object:
```shell
PG_BOSS_NEW_OPTIONS='{"connectionString":"...db url...","jobExpirationInSeconds":60,"maxRetries":3}'
```
### Custom Env Var Validations
If your code requires some environment variables, you usually want to ensure that they are correctly defined. In Wasp, you can define your environment variables validation by defining a [Zod object schema](https://zod.dev/?id=basic-usage) and telling Wasp to use it.
:::info[What is Zod?]
[Zod](https://zod.dev/) is a library that lets you define what you expect from your data. For example, you can use Zod to define that:
- A value should be a string that's a valid email address.
- A value should be a number between 0 and 100.
- ... and much more.
:::
Take a look at an example of defining env vars validation:
```ts title="src/env.ts"
import * as z from "zod"
import { defineEnvValidationSchema } from "wasp/env"
export const serverEnvValidationSchema = defineEnvValidationSchema(
z.object({
STRIPE_API_KEY: z.string({
required_error: "STRIPE_API_KEY is required.",
}),
})
)
export const clientEnvValidationSchema = defineEnvValidationSchema(
z.object({
REACT_APP_NAME: z.string().default("TODO App"),
})
)
```
The `defineEnvValidationSchema` function ensures your Zod schema is type-checked.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { clientEnvValidationSchema, serverEnvValidationSchema } from "./src/env" with { type: "ref" }
export default app({
name: "myApp",
client: {
envValidationSchema: clientEnvValidationSchema,
},
server: {
envValidationSchema: serverEnvValidationSchema,
},
// ...
})
```
You defined schemas for both the client and the server env vars and told Wasp to use them. Wasp merges your env validation schemas with the built-in env vars validation schemas when it validates the `process.env` object on the server and the `import.meta.env` object on the client.
This means you can use the `env` object to access **your env vars** like this:
```ts title="src/stripe.ts"
import { env } from "wasp/server"
const stripeApiKey = env.STRIPE_API_KEY
```
Read more about the env object in the [API Reference](#api-reference).
### API Reference
There are **Wasp-defined** and **user-defined** env vars. Wasp already comes with built-in validation for Wasp-defined env vars. For your env vars, you can define your own validation.
#### Client Env Vars {#client-env-vars-api}
##### User-defined env vars validation
You can define your client env vars validation like this:
```ts title="src/env.ts"
import * as z from "zod"
import { defineEnvValidationSchema } from "wasp/env"
export const envValidationSchema = defineEnvValidationSchema(
z.object({
REACT_APP_ANALYTICS_ID: z.string({
required_error: "REACT_APP_ANALYTICS_ID is required.",
}),
})
)
```
The `defineEnvValidationSchema` function ensures your Zod schema is type-checked.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { envValidationSchema } from "./src/env" with { type: "ref" }
export default app({
name: "myApp",
client: {
envValidationSchema,
},
// ...
})
```
Wasp merges your env validation schemas with the built-in env vars validation schemas when it validates the `import.meta.env` object.
##### Accessing env vars in client code
You can access both **Wasp-defined** and **user-defined** client env vars in your client code using the `env` object:
```ts title="src/App.ts"
import { env } from "wasp/client"
// Wasp-defined
const apiUrl = env.REACT_APP_API_URL
// User-defined
const analyticsId = env.REACT_APP_ANALYTICS_ID
```
You can use `import.meta.env.REACT_APP_SOME_VAR_NAME` directly in your code. We don't recommend this since `import.meta.env` isn't validated and missing env vars can cause runtime errors.
#### Server Env Vars
##### User-defined env vars validation
You can define your env vars validation like this:
```ts title="src/env.ts"
import * as z from "zod"
import { defineEnvValidationSchema } from "wasp/env"
export const envValidationSchema = defineEnvValidationSchema(
z.object({
STRIPE_API_KEY: z.string({
required_error: "STRIPE_API_KEY is required.",
}),
})
)
```
The `defineEnvValidationSchema` function ensures your Zod schema is type-checked.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { envValidationSchema } from "./src/env" with { type: "ref" }
export default app({
name: "myApp",
server: {
envValidationSchema,
},
// ...
})
```
Wasp merges your env validation schemas with the built-in env vars validation schemas when it validates the `process.env` object.
##### Accessing env vars in server code
You can access both **Wasp-defined** and **user-defined** client env vars in your client code using the `env` object:
```ts title="src/stripe.ts"
import { env } from "wasp/server"
// Wasp-defined
const serverUrl = env.WASP_SERVER_URL
// User-defined
const stripeApiKey = env.STRIPE_API_KEY
```
You can use `process.env.SOME_SECRET` directly in your code. We don't recommend this since `process.env` isn't validated and missing env vars can cause runtime errors.
## Project Setup / Testing
:::info
Wasp is in beta, so keep in mind there might be some kinks / bugs, and possibly some changes with testing support in the future. If you encounter any issues, reach out to us on [Discord](https://discord.gg/rzdnErX) and we will make sure to help you out!
:::
### Testing Your React App
Wasp enables you to quickly and easily write both unit tests and React component tests for your frontend code. Because Wasp uses [Vite](https://vitejs.dev/), we support testing web apps through [Vitest](https://vitest.dev/).
Make sure your `devDependencies` include the Vitest dependency. Wasp starters come with Vitest included:
```json title="package.json"
{
"devDependencies": {
"vitest": "^4.1.9"
}
}
```
Testing Libraries
[`vitest`](https://www.npmjs.com/package/vitest): Unit test framework with native Vite support.
[`@vitest/ui`](https://www.npmjs.com/package/@vitest/ui): A nice UI for seeing your test results.
[`jsdom`](https://www.npmjs.com/package/jsdom): A web browser test environment for Node.js.
[`@testing-library/react`](https://www.npmjs.com/package/@testing-library/react) / [`@testing-library/jest-dom`](https://www.npmjs.com/package/@testing-library/jest-dom): Testing helpers.
[`msw`](https://www.npmjs.com/package/msw): A server mocking library.
#### Writing Tests
For Wasp to pick up your tests, they should be placed within the `src` directory and use an extension that matches [these glob patterns](https://vitest.dev/config#include). Some of the file names that Wasp will pick up as tests:
- `yourFile.test.ts`
- `YourComponent.spec.jsx`
Within test files, you can import your other source files as usual. For example, if you have a component `Counter.jsx`, you test it by creating a file in the same directory called `Counter.test.jsx` and import the component with `import Counter from './Counter'`.
#### Running Tests
Running `wasp test client` will start Vitest in watch mode and recompile your Wasp project when changes are made.
- If you want to see a real-time UI, pass `--ui` as an option.
- To run the tests just once, use `wasp test client run`.
All arguments after `wasp test client` are passed directly to the Vitest CLI, so check out [their documentation](https://vitest.dev/guide/cli.html) for all of the options.
:::warning[Be Careful]
You should not run `wasp test` while `wasp start` is running. Both will try to compile your project to `.wasp/out`.
:::
#### React Testing Helpers
Wasp provides several functions to help you write React tests:
- `renderInContext`: Takes a React component, wraps it inside a `QueryClientProvider` and `Router`, and renders it. This is the function you should use to render components in your React component tests.
```js
import { renderInContext } from "wasp/client/test";
renderInContext();
```
- `mockServer`: Sets up the mock server and returns an object containing the `mockQuery` and `mockApi` utilities. This should be called outside of any test case, in each file that wants to use those helpers.
```js
import { mockServer } from "wasp/client/test";
const { mockQuery, mockApi } = mockServer();
```
- `mockQuery`: Takes a Wasp [query](https://wasp.sh/docs/data-model/operations/queries) to mock and the JSON data it should return.
```js
import { getTasks } from "wasp/client/operations";
mockQuery(getTasks, []);
```
- Helpful when your component uses `useQuery`.
- Behind the scenes, Wasp uses [`msw`](https://npmjs.com/package/msw) to create a server request handle that responds with the specified data.
- Mock are cleared between each test.
- `mockApi`: Similar to `mockQuery`, but for [APIs](https://wasp.sh/docs/advanced/apis). Instead of a Wasp query, it takes a route containing an HTTP method and a path.
```js
import { HttpMethod } from "wasp/client";
mockApi({ method: HttpMethod.Get, path: "/foor/bar" }, { res: "hello" });
```
### Testing Your Server-Side Code
Wasp currently does not provide a way to test your server-side code, but we will be adding support soon. You can track the progress at [this GitHub issue](https://github.com/wasp-lang/wasp/issues/110) and express your interest by commenting.
### Examples
You can see some tests in a Wasp project [here](https://github.com/wasp-lang/wasp/blob/release/waspc/examples/todoApp/src/pages/auth/helpers.test.ts).
#### Client Unit Tests
```ts title="src/helpers.ts"
import { type Task } from "wasp/entities";
export function areThereAnyTasks(tasks: Task[]): boolean {
return tasks.length !== 0;
}
```
```ts title="src/helpers.test.ts"
import { test, expect } from "vitest";
import { areThereAnyTasks } from "./helpers";
test("areThereAnyTasks", () => {
expect(areThereAnyTasks([])).toBe(false);
});
```
#### React Component Tests
```tsx title="src/Todo.tsx"
import { useQuery, getTasks } from "wasp/client/operations";
const Todo = (_props: {}) => {
const { data: tasks } = useQuery(getTasks);
return (
{tasks &&
tasks.map((task) => (
{task.description}
))}
);
};
```
```tsx title="src/Todo.test.tsx"
import { test, expect } from "vitest";
import { screen } from "@testing-library/react";
import { mockServer, renderInContext } from "wasp/client/test";
import { getTasks } from "wasp/client/operations";
import Todo from "./Todo";
const { mockQuery } = mockServer();
const mockTasks = [
{
id: 1,
description: "test todo 1",
isDone: true,
userId: 1,
},
];
test("handles mock data", async () => {
mockQuery(getTasks, mockTasks);
renderInContext();
await screen.findByText("test todo 1");
expect(screen.getByRole("checkbox")).toBeChecked();
screen.debug();
});
```
#### Testing With Mocked APIs
```tsx title="src/Todo.tsx"
import { type Task } from "wasp/entities";
import { api } from "wasp/client/api";
const Todo = (_props: {}) => {
const [tasks, setTasks] = useState([]);
useEffect(() => {
api.get("/tasks").json()
.then((tasks) => setTasks(tasks))
.catch((err) => window.alert(err));
});
return (
{tasks &&
tasks.map((task) => (
{task.description}
))}
);
};
```
```tsx title="src/Todo.test.tsx"
import { test, expect } from "vitest";
import { screen } from "@testing-library/react";
import { mockServer, renderInContext } from "wasp/client/test";
import Todo from "./Todo";
const { mockApi } = mockServer();
const mockTasks = [
{
id: 1,
description: "test todo 1",
isDone: true,
userId: 1,
},
];
test("handles mock data", async () => {
mockApi("/tasks", mockTasks);
renderInContext();
await screen.findByText("test todo 1");
expect(screen.getByRole("checkbox")).toBeChecked();
screen.debug();
});
```
## Project Setup / Dependencies
In a Wasp project, dependencies are defined in a standard way for JavaScript projects: using the [package.json](https://docs.npmjs.com/cli/configuring-npm/package-json) file, located at the root of your project. You can list your dependencies under the `dependencies` or `devDependencies` fields.
#### Adding a New Dependency
To add a new package, like `date-fns` (a great date handling library), you use `npm`:
```bash
npm install date-fns
```
This command will add the package in the `dependencies` section of your `package.json` file.
You will notice that there are some other packages in the `dependencies` section, like `react` and `wasp`. These are the packages that Wasp uses internally, and you should not modify or remove them.
#### Using Packages that are Already Used by Wasp Internally
Wasp internally uses certain dependencies (e.g. React, Prisma, Vite) with specific versions. By default, you cannot specify a different version for these packages - if you try, you'll get an error telling you which version Wasp requires.
##### Overriding Wasp's Dependencies (Advanced)
If you need to use a different version of a Wasp-managed dependency, you can override it using the `wasp.overriddenDeps` field in your `package.json`. This is an advanced feature intended for:
- Testing newer versions of dependencies before Wasp officially supports them
- Working around bugs in a specific dependency version
- Using older versions for compatibility reasons
:::caution
This functionality is intended to give you control when absolutely necessary, but it comes with risks.
We recommend that you **don't** use override in production projects. We don't test Wasp with different versions of our dependencies, and we don't guarantee functionality or stability. Incompatibilities might be big and obvious, but they can also be subtle and indirect.
When you override dependencies, it's up to you to test your app thoroughly and validate that it works as expected. If issues arise from using overridden versions, it's also up to you to deal with them.
:::
:::tip
If you find the need to override any dependency, we'd appreciate for you to [post an issue on GitHub](https://github.com/wasp-lang/wasp/issues/new/choose), or a [message on our Discord](https://discord.gg/rzdnErX), explaining your usecase. This will make us aware of your needs, and helps us prioritize giving you a supported solution faster.
:::
To override a dependency, add the `wasp` field to your `package.json` with an `overriddenDeps` object. The keys are the package names, and the values are **what Wasp currently requires** (not your desired version):
**Before**
```json title="package.json"
{
"dependencies": {
"react": "19.2.1",
"react-dom": "19.2.1"
}
}
```
**After**
```json title="package.json"
{
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
},
"wasp": {
"overriddenDeps": {
"react": "19.2.1",
"react-dom": "19.2.1"
}
}
}
```
In this example:
- You want to use React 18.2.0 (specified in `dependencies`)
- Wasp requires React 19.2.1 (specified in `overriddenDeps`)
- By declaring this, you acknowledge you're deviating from Wasp's tested version
When Wasp updates its requirements in a new release, you'll need to update your `overriddenDeps` values to match. This ensures you consciously acknowledge each change.
:::note
If you need the override to apply to transitive dependencies as well (dependencies of your dependencies), you can use npm's built-in [`overrides`](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#overrides) feature alongside `wasp.overriddenDeps`.
:::
#### Supply Chain Protection
New Wasp projects include an `.npmrc` file with [`min-release-age`](https://docs.npmjs.com/cli/v11/using-npm/config#min-release-age) set to **7 days** by default. This prevents npm from installing any package version that was published less than 7 days ago, which helps protect against [supply chain attacks](https://en.wikipedia.org/wiki/Supply_chain_attack). Malicious packages are typically detected and removed within hours of publication, so by adding a short delay there's a much smaller chance of being targeted by these attacks.
If you need to install a recently published package, you can temporarily override this by passing the flag directly:
```bash
npm install some-package --min-release-age=0
```
Or you can adjust the value in your project's `.npmrc` file.
## Project Setup / Custom Vite Config
Wasp uses [Vite](https://vitejs.dev/) to serve the client during development and bundling it for production. If you want to customize the Vite config, you can do that by editing the `vite.config.ts` file in your project root directory.
### Required Configuration
You have **full control** over your `vite.config.ts` file. Wasp doesn't manage this file internally. Instead, you must import and use the `wasp()` plugin from `wasp/client/vite` in your Vite configuration. This plugin provides all the essential Wasp features:
- Configuration required for Wasp full-stack apps to work.
- Environment variables validation.
- Prevention of server imports in client code.
- TypeScript type checking during production builds.
Here's the minimal required configuration:
```ts title="vite.config.ts"
import { wasp } from 'wasp/client/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [wasp()],
})
```
:::warning[Plugin order]
The `wasp()` plugin must be the **first** plugin in the `plugins` array. Any other plugins (like Tailwind CSS) should be added after it.
:::
### Enforced Options
The `wasp()` plugin enforces certain Vite config values that Wasp needs to function correctly. If you set any of these in your `vite.config.ts`, Wasp will throw an error asking you to remove them.
| Option | Internal value | Why you can't customize it |
| -------------- | ------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| `base` | Based on the [`client.baseDir`](https://wasp.sh/docs/project/client-config#base-directory) option | Wasp sets the React Router's `basename` to the same value. |
| `envPrefix` | `"REACT_APP_"` | Wasp's environment variable validation depends on this prefix. |
| `build.outDir` | `".wasp/out/web-app/build"` | Build artifacts must go to the location Wasp expects for deployment. |
### Customization
You can add additional configuration and plugins as needed. The `wasp()` plugin will use your config and merge it with the built-in defaults.
Vite config customization can be useful for things like:
- Adding additional Vite plugins.
- Customizing the dev server behavior.
- Customizing the build process.
### Plugin Options
The `wasp()` plugin accepts options allowing you to customize the underlying React plugin behavior if needed:
```ts title="vite.config.ts"
import { wasp } from "wasp/client/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
wasp({
reactOptions: {
// Pass any @vitejs/plugin-react options here
},
}),
],
});
```
### Examples
Below are some examples of how you can customize the Vite config.
#### Changing the Dev Server Behaviour
If you want to stop Vite from opening the browser automatically when you run `wasp start`, you can do that by customizing the `open` option.
```ts title="vite.config.ts"
import { wasp } from "wasp/client/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [wasp()],
server: {
open: false,
},
});
```
#### Custom Dev Server Port
You have access to all of the [Vite dev server options](https://vitejs.dev/config/server-options.html) in your custom Vite config. You can change the **client** dev server port by setting the `port` option. To change the Wasp **server** port, see the [`PORT` server env var](https://wasp.sh/docs/project/env-vars#server-general-configuration).
```ts title="vite.config.ts"
import { wasp } from "wasp/client/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [wasp()],
server: {
port: 4000,
},
});
```
```env title=".env.server"
WASP_WEB_CLIENT_URL=http://localhost:4000
```
:::warning[Changing the client dev server port]
Be careful when changing the client dev server port, you'll need to update the `WASP_WEB_CLIENT_URL` env var in your `.env.server` file.
:::
#### Editing from the Chrome DevTools {#devtools-workspace}
Chrome DevTools support [mapping a page's resources to a folder](https://developer.chrome.com/docs/devtools/workspaces), so any changes you make in the browser are reflected back to your files. To enable it, you can use their Vite plugin: [`vite-plugin-devtools-json`](https://github.com/ChromeDevTools/vite-plugin-devtools-json).
1. Install the plugin as a **dev dependency**:
```bash
npm i -D vite-plugin-devtools-json
```
2. Extend your `vite.config.{ts,js}`:
```ts title="vite.config.ts"
import { wasp } from "wasp/client/vite";
import { defineConfig } from "vite";
import devtoolsJson from "vite-plugin-devtools-json";
export default defineConfig({
plugins: [wasp(), devtoolsJson({ root: import.meta.dirname })],
});
```
3. Start your app with `wasp start`, open **Chrome DevTools โ Sources โ Workspace** and you should see your project automatically mapped. Changes you make in DevTools now save to disk and Vite's HMR updates the browser instantly!
:::tip[Path normalisation]
The latest version of `vite-plugin-devtools-json` includes Windows, WSL and Docker Desktop path fixes contributed by the Wasp community โ make sure you are on version 0.4.0 or greater.
:::
### API Reference
```ts title="vite.config.ts"
import { wasp } from "wasp/client/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
wasp({
reactOptions: {
// ...
},
}),
],
});
```
The `wasp()` plugin accepts the following options:
- ##### `reactOptions: ReactOptions` optional {#reactoptions-reactoptions-}
Object to customize the underlying [`@vitejs/plugin-react`](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react) plugin.
This allows you to configure React-specific options like Babel plugins, Fast Refresh settings, and JSX configuration.
## Deployment / Introduction
After developing your app locally on your machine, the next step is to deploy it to the web so that others can access it.
In this section, we'll walk you through the steps to deploy your Wasp app.
#### Wasp app structure
Before we start, let's understand what Wasp generates when it builds your app.
What we call a "Wasp app" consists of three different parts:
- **Client app**
- It's a single-page application (SPA), built using [React](https://react.dev/). It's what the user sees and interacts with.
- It's usually served by some static file server or you can host it on a CDN like Cloudflare or Netlify.
- **Server app**:
- The backend of your app, built using [Express](https://expressjs.com/) on Node.js.
- It handles requests from the client app, interacts with the database, and returns responses.
- It comes with a ready-to-use `Dockerfile` so you can easily package it and deploy it anywhere where Docker is supported.
- **Database**:
- Wasp uses [PostgreSQL](https://www.postgresql.org/) as its production database.
- You can host the database on your own server or use a cloud service.

Data flow in a typical deployed Wasp app where all three parts are deployed separately
The thing to take away from this: the client app and server app are separate applications that communicate with each other over HTTP. This means you can deploy them on the same or different servers, depending on your needs.
We'll show you different ways of how deploy your app in the [deployment methods](https://wasp.sh/docs/deployment/deployment-methods/overview) section.
Server needs to be able to communicate with the database, we'll show you how to set that up using [env variables](https://wasp.sh/docs/deployment/env-vars).
#### Deploying your app
In the following sections, we'll go through all the different things you need to know about deployment:
- How [env variables](https://wasp.sh/docs/deployment/env-vars) work in production - they are different than using .env files in development.
- Production [database setup](https://wasp.sh/docs/deployment/database) - how migrations work, how to connect to the database, etc.
- Different deployment methods (using [Wasp's CLI](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/overview), [cloud services](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers), [self-hosting](https://wasp.sh/docs/deployment/deployment-methods/self-hosted), etc.)
- How to [set up CI/CD](https://wasp.sh/docs/deployment/ci-cd) for your app - automatically deploy your app when you push to your Git repository.
- Some [extras](https://wasp.sh/docs/deployment/extras) like custom domains, CDN, etc.
## Deployment / Env Variables
We talked about environment variables in the [project setup section](https://wasp.sh/docs/project/env-vars). If you haven't read it, make sure to check it out first. In this section, we'll talk about environment variables in the context of deploying the app.
While developing our app on our machine, we had the option of using `.env.client` and `.env.server` files which made it easy to define and manage env vars.
However, when we are deploying our app, **`.env.client` and `.env.server` files will be ignored, and we need to provide env vars differently.**

#### Client Env Vars
During the build process, client env vars are injected into the client Javascript code, making them public and readable by anyone. Therefore, you should **never store secrets in them** (such as secret API keys).
When building for production, the `.env.client` file will be ignored, since it is meant to be used only during development. Instead, you should provide the production client env vars directly to the build command that turns client code into static files.
Make sure to check the [required client env vars](https://wasp.sh/docs/project/env-vars#client-general-configuration) and set them when building for production, the build will fail if any required env vars are missing.
```shell
REACT_APP_API_URL= REACT_APP_SOME_OTHER_VAR_NAME=someothervalue npx vite build
```
Also, notice **that you can't and shouldn't provide client env vars to the client code by setting them on the hosting provider** (unlike providing server env vars to the server app, in that case this is how you should do it). Your client code will ignore those, as at that point client code is just static files.
:::info[How it works]
What happens behind the scenes is that Wasp will replace all occurrences of `import.meta.env.REACT_APP_SOME_VAR_NAME` in your client code with the env var value you provided. This is done during the build process, so the value is injected into the static files produced from the client code.
Read more about it in Vite's [docs](https://vitejs.dev/guide/env-and-mode.html#production-replacement).
:::
#### Server Env Vars
When building your Wasp app for production `.env.server` will be ignored, since it is meant to be used only during development.
You can provide production env vars to your server code in production by defining them and making them available on the server where your server code is running.
:::caution[Set the required env vars]
Make sure to go through [all the required server env vars](https://wasp.sh/docs/project/env-vars#server-general-configuration) like `DATABASE_URL`, `WASP_WEB_CLIENT_URL`, `WASP_SERVER_URL` etc. and set them up in your production environment.
While some env vars like `WASP_WEB_CLIENT_URL` and `WASP_SERVER_URL` have default values in development, they are **required in production** and must be explicitly set.
**If you are using the [Wasp CLI](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/overview)** deployment method, Wasp will set the general configuration env vars for you, but you will need to set the rest of the env vars yourself (like the ones for OAuth auth methods or any other custom env vars you might have defined).
:::
Setting server env variables up will highly depend on where you are deploying your server, but in general it comes down to defining the env vars via mechanisms that your hosting provider provides.
For example, if you deploy your server to [Fly](https://fly.io), you can define them using the `fly` CLI tool:
```shell
fly secrets set SOME_VAR_NAME=somevalue
```
We talk about specific providers in the [Cloud Providers section](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers) or the [self-hosted deployment section](https://wasp.sh/docs/deployment/deployment-methods/self-hosted).
## Deployment / Database
In this section, we'll discuss what happens with the database when your app goes live. When you develop your app locally, you probably use a local dev database (started with `wasp start db` or some other way). However, when it's time to deploy your app, you'll need to set up a production database.
#### Production database requirements
The server app that Wasp generates uses a PostgreSQL database. The only requirement from Wasp's point of view is that the database is accessible from the server via the `DATABASE_URL` server env variable.
It can be a PostgreSQL database running on the same server as the server app, or it can be a managed PostgreSQL database service like [Fly Postgres](https://fly.io/docs/postgres/), [AWS RDS](https://aws.amazon.com/rds/), or some other service.
### Migrations
Every time you make a change in your [Prisma schema](https://wasp.sh/docs/data-model/prisma-file) e.g. adding a new model, changing a field type, etc., you need to create a migration. Migrations are some code that describes the change you made in the schema, and they are used to apply the change to the database.
The benefit of migrations is that you can apply the same change to multiple databases. If there are multiple people working on the project, they can all apply the same changes to their local databases. When you deploy the app to production, the same chaanges are applied to the production database.
#### Creating migrations
After you made a change in the Prisma schema, you can create a migration by running the following command:
```bash
wasp db migrate-dev
```
This command will create a new migration in the `migrations` directory. The migration is a set of SQL commands that describe the change you made in the schema.
#### Applying migrations
**In development**, the migrations are applied as soon as you run the `wasp start` command.
**In production**, the server app first checks if there are any new migrations that need to be applied, and if there are, it applies them before starting the server. This way, the database schema is always in sync with the Prisma schema.
:::note[How it works]
In the built server app, there are two npm scripts: `start` and `start-production`. The `start` script is used in development, and the `start-production` script is used in production. The `start-production` script first applies any pending migrations before starting the server.
:::
The migrations might fail to apply if there is a conflict with the existing data in the database. In that case, you'll need to fix the migration and try again.
#### Debugging failed migrations
If a migration fails to apply, the server app will log the error message and stop. You should then connect to the production database and see what went wrong. If you check the `_prisma_migrations` table, you'll see the failed migration there.
You can try resolving the erorr e.g. if you tried adding a `@unique` constraint to a field that already has duplicate values:
1. Remove any duplicate values from the database
2. Remove the failed migration from the `_prisma_migrations` table
3. Try applying the migration again by restarting the server app
:::tip[Viewing the _prisma_migrations table]
You can't use the `wasp db studio` command to view the `_prisma_migrations` table in the production database, but you can use a database management tool like [DBeaver](https://dbeaver.io/) or [pgAdmin](https://www.pgadmin.org/).
:::
### Connect to the production database
**In development**, you can use the `wasp db studio` command to open a web-based database management tool that allows you to inspect the database.
You can use the same tool to inspect the **production database**, but you'll need to set the `DATABASE_URL` env variable to point to the production database. Set the `DATABASE_URL` env variable in your terminal before running the `wasp db studio` command:
```bash
DATABASE_URL="postgresql://user:password@host:port/dbname" wasp db studio
```
:::caution[Be careful with the DATABASE_URL env variable]
Setting the `DATABASE_URL` env variable in the `.env.server` file to point to your production database also works, but then you might forget to remove it and you could accidentally make changes to the production database when you run `wasp start` in development.
That's why we recommend setting the `DATABASE_URL` env variable in the terminal to avoid this.
:::
If you are looking how to connect to a Fly.io production database, we wrote a guide on how to do that:
[Guide](https://wasp.sh/docs/guides/debugging/db-studio-fly-io)
#### [Database Studio with Fly.io ยป](https://wasp.sh/docs/guides/debugging/db-studio-fly-io)
[Connect to your Fly.io production database and run wasp db studio](https://wasp.sh/docs/guides/debugging/db-studio-fly-io)
## Deployment / Testing the build locally
`wasp build start` lets you test your production build locally before deployment, ensuring everything works correctly before going live.
This command takes the output of `wasp build` and starts a local server to run it. That means that you can test using the same optimized code that would be deployed to production. You also configure it with the same environment variables you'd use in production, which helps you catch configuration issues before deploying.
While it's not identical to a real production environment, it's the closest you can get to testing your deployed app without actually deploying it.
:::warning[This is not a deployment command]
`wasp build start` is only intended for testing your `wasp build` output locally, and is not designed for serving your app in production. For that, check out our [deployment guide](https://wasp.sh/docs/deployment/intro).
:::
### Usage
```bash
# Start a local database, copy the connection URL
wasp start db
# Start the local production build server
# (this is an example, you'll probably need to add more environment variables)
wasp build start --server-env DATABASE_URL= --server-env JWT_SECRET=
```
:::tip
For `JWT_SECRET`, you can generate a random secret here: Generate secret.
You might need to pass other environment variables as well, depending on your app's configuration. Check our [Environment variables reference](https://wasp.sh/docs/project/env-vars) for more details.
:::
This command will:
- Start a local server serving your production build (the output of `wasp build`).
- Use only the environment variables you set explicitly.
- Use the same bundled assets that would be deployed.
- Run in production mode with optimizations enabled.
### Why?
The main reason for using `wasp build start` is to catch dependencies on your local development environment that might not work in production.
For example, your app might rely on environment variables that are set in your local `.env` files. `wasp start` by default will read these files and use them. While this makes it easy to develop your app locally, it also makes it easy to lose track of which environment variables your app actually needs in production.
`wasp build start` forces you to explicitly specify the environment variables your app needs to run in production. This helps you double-check which ones you also need to set in your deployment environment for the app to work correctly.
Your code might also depend on some development-only features in your libraries, such as React development or strict mode. `wasp build start` runs your app as they would for your users, which means that these features are disabled. This helps you catch issues that might only appear in production.
You should treat this command as the last check before deploying your app, confirming that you know all required environment variables and that integrations behave as expected with production settings on. It is also the best way to reproduce issues that only appear in production, which can be very useful for debugging.
### Differences from `wasp start`
| Aspect | `wasp start` | `wasp build start` |
| ---------------------------------------- | ------------------- | ---------------------------------------------------------------------------- |
| Runs your app for general production use | **No** | **No** (check our [deployment guide](https://wasp.sh/docs/deployment/intro)) |
| Intended for | Local development | Local production testing |
| Server environment | Node.js | Node.js in a Docker container |
| Client environment | Static server | Static server |
| Assets | Served individually | Bundled and minified |
| React dev mode | Enabled | Disabled |
| Hot reload | Enabled | Disabled |
| Source maps | Enabled | Disabled |
| Debugging support | Full | Limited |
| Performance | Slower | Normal |
### Passing environment variables
You must manually specify any environment variables that your app needs to run in production. This is crucial because the production build may require different configurations from the development build. This helps you take note of which ones you also need to set in your deployment environment for the app to work correctly.
Environment variables include database URLs, API keys, and any other configuration settings necessary for your app to function correctly. You can usually check out your [`.env` files](https://wasp.sh/docs/project/env-vars#dotenv-files) to see what environment variables your app expects. You can read more about environment variables in Wasp in the [environment variables guide](https://wasp.sh/docs/project/env-vars).
The only exception is the environment variables that configure your app's client and server URLs (`WASP_WEB_CLIENT_URL`, `WASP_SERVER_URL`, and `REACT_APP_API_URL`). Because `wasp build start` knows that it's running the app on your local workstation, it can fill them out for you automatically.
#### Which values should I use when testing?
- Do not use real production secrets or endpoints.
- Prefer staging/sandbox credentials and services that mirror production (e.g., Stripe test keys, a staging DB, or an isolated local DB with realistic data).
- Keep config parity with production: same feature flags, callbacks/redirect URLs, and optional vars set/unset as in prod.
Example:
```bash
wasp build start --server-env-file .env.staging --client-env-file .env.client.staging
```
#### Server environment variables
Use `--server-env` to specify environment variables for the server:
```bash
wasp build start --server-env DATABASE_URL=postgresql://localhost:5432/myapp
```
You can specify multiple server environment variables:
```bash
wasp build start --server-env DATABASE_URL=postgresql://localhost:5432/myapp --server-env JWT_SECRET=my-secret-key
```
You can also point to an `.env` file to load environment variables:
```bash
wasp build start --server-env-file .env.production
```
:::warning
Do not commit your `.env` files with sensitive information to your version control system. Use `.gitignore` to exclude them.
:::
#### Client environment variables
Use `--client-env` to specify environment variables for the client:
```bash
wasp build start --client-env REACT_APP_GOOGLE_ANALYTICS_ID=GA-123456
```
Multiple client environment variables:
```bash
wasp build start --client-env REACT_APP_GOOGLE_ANALYTICS_ID=GA-123456 --client-env REACT_APP_PLAUSIBLE_ID=PLAUSIBLE-123456
```
You can also point to an `.env` file for client variables:
```bash
wasp build start --client-env-file .env.client.production
```
:::warning
Do not commit your `.env` files with sensitive information to your version control system. Use `.gitignore` to exclude them.
:::
## Deployment / Deployment Methods / Overview
Wasp apps are full-stack apps that consist of:
- A Node.js server.
- A static client.
- A PostgreSQL database.
To make deploying as smooth as possible, Wasp also offers a single-command deployment called **Wasp Deploy**.
[Documentation](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/overview)
#### [Wasp Deploy ยป](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/overview)
[One-command deployment & redeployment](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/overview)
But even when not using Wasp Deploy, you can deploy each part **anywhere** where you can usually deploy Node.js apps or static apps. For example, you can deploy your client on [Netlify](https://www.netlify.com/), the server on [Fly.io](https://fly.io/), and the database on [Neon](https://neon.tech/).
You can read our guides on how to deploy your Wasp app to different platforms, both from cloud providers and on your own infrastructure:
[Documentation](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers)
#### [Cloud Providers ยป](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers)
[Deploy your Wasp app to various cloud platforms](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers)
[Documentation](https://wasp.sh/docs/deployment/deployment-methods/self-hosted)
#### [Self-hosted ยป](https://wasp.sh/docs/deployment/deployment-methods/self-hosted)
[Use your own servers to host your app](https://wasp.sh/docs/deployment/deployment-methods/self-hosted)
Regardless of how you choose to deploy your app (i.e., manually or using the Wasp CLI), you'll need to know about some common patterns covered below.
:::tip[Deployed? Get some swag! ๐๐]
Do you have a Wasp app running in production? If yes, we'd love to send some swag your way! All you need to do is fill [this form](https://e44cy1h4s0q.typeform.com/to/EPJCwsMi) out and we'll make it happen.
:::
### Customizing the Dockerfile
By default, Wasp generates a multi-stage Dockerfile. This file is used to build and run a Docker image with the Wasp-generated server code. It also runs any pending migrations.
You can **add extra steps to this multi-stage `Dockerfile`** by creating your own `Dockerfile` in the project's root directory. If Wasp finds a Dockerfile in the project's root, it appends its contents at the *bottom* of the default multi-stage Dockerfile.
Since the last definition in a Dockerfile wins, you can override or continue from any existing build stages. You can also choose not to use any of our build stages and have your own custom Dockerfile used as-is.
A few things to keep in mind:
- If you override an intermediate build stage, no later build stages will be used unless you reproduce them below.
- The generated Dockerfile's content is dynamic and depends on which features your app uses. The content can also change in future releases, so please verify it from time to time.
- Make sure to supply `ENTRYPOINT` in your final build stage. Your changes won't have any effect if you don't.
Read more in the official Docker docs on [multi-stage builds](https://docs.docker.com/build/building/multi-stage/).
To see what your project's (potentially combined) Dockerfile will look like, run:
```shell
wasp dockerfile
```
Join our [Discord](https://discord.gg/rzdnErX) if you have any questions, or if you need more customization than this hook provides.
## Deployment / Deployment Methods / Wasp Deploy / Overview
Wasp CLI can deploy your full-stack application with a single command. The command automates the manual deployment process and is the recommended way of deploying Wasp apps.
It looks like this:
```shell
wasp deploy launch my-wasp-app
```
The `wasp deploy` command sets up all the necessary services on the provider, builds your Wasp app, and deploys it.
#### Supported Providers
Wasp Deploy supports automated deployment to the following providers:
#### [Fly.io ยป](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/fly)
#### [Railway ยป](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/railway)
Click on each provider for more details.
## Deployment / Deployment Methods / Wasp Deploy / Fly.io
[Fly.io](https://fly.io/) is a platform for running containerized apps and microservices on servers around the world. It makes deploying and managing your apps straightforward with minimal setup.
### Prerequisites
To deploy to Fly.io using Wasp CLI:
1. Create a [Fly.io](https://fly.io/) account
2. Fly requires you to add a payment method before you can deploy more than two Fly apps. To deploy Wasp apps, you need three Fly apps: the client, the server, and the database.
3. Install the [`fly` CLI](https://fly.io/docs/hands-on/install-flyctl/) on your machine.
### Deploying
Using the Wasp CLI, you can easily deploy a new app to [Fly.io](https://fly.io) with just a single command:
```shell
wasp deploy fly launch my-wasp-app dfw
```
Please do not CTRL-C or exit your terminal while the commands are running.
Two things to keep in mind:
1. Your app name (for example `my-wasp-app`) must be **unique** across all of Fly or deployment will fail.
2. If your account is a member of **more than one organization** on Fly.io, you will need to specify under which one you want to execute the command. To do that, provide an additional `--org ` option. You can find out the names (slugs) of your organizations by running `fly orgs list`.
The `launch` command uses the app basename `my-wasp-app` and deploy it to the `dfw` region (`dfw` is short for *Dallas, Texas (US)*). Read more about Fly.io regions [here](#flyio-regions).
The basename is used to create all three app tiers, resulting in three separate apps in your Fly dashboard:
- `my-wasp-app-client`
- `my-wasp-app-server`
- `my-wasp-app-db`
You'll notice that Wasp creates two new files in your project root directory:
- `fly-server.toml`
- `fly-client.toml`
You should include these files in your version control so that you can deploy your app with a single command in the future.
When you run the `launch` command, Wasp CLI knows how to connect different parts of your Wasp app together, so it sets up the required environment variables for your server app:
1. `WASP_WEB_CLIENT_URL` and `WASP_SERVER_URL` which are required to connect your client and server apps.
2. `DATABASE_URL` which is required to connect your server app to the database.
3. `JWT_SECRET` which is required for authentication to work.
If your app requires any additional environment variables, use the `wasp deploy fly cmd secrets set` command. Read more in the [API Reference](#flyio-cli-environment-variables) section.
If you want to automate the deployment process, check out the [CI/CD Deployment](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/ci-cd) page to re-deploy your app on each commit as part of your CI/CD pipeline.
### Using a Custom Domain For Your App {#custom-domain}
Setting up a custom domain is a three-step process:
1. You need to add your domain to your Fly client app. You can do this by running:
```shell
wasp deploy fly cmd --context client certs create mycoolapp.com
```
:::note[Use Your Domain]
Make sure to replace `mycoolapp.com` with your domain in all of the commands mentioned in this section.
:::
This command will output the instructions to add the DNS records to your domain. It will look something like this:
```shell-session
You can direct traffic to mycoolapp.com by:
1: Adding an A record to your DNS service which reads
A @ 66.241.1XX.154
You can validate your ownership of mycoolapp.com by:
2: Adding an AAAA record to your DNS service which reads:
AAAA @ 2a09:82XX:1::1:ff40
```
2. You need to add the DNS records for your domain:
*This will depend on your domain provider, but it should be a matter of adding an A record for `@` and an AAAA record for `@` with the values provided by the previous command.*
3. You need to set your domain as the `WASP_WEB_CLIENT_URL` environment variable for your server app:
```shell
wasp deploy fly cmd --context server secrets set WASP_WEB_CLIENT_URL=https://mycoolapp.com
```
We need to do this to keep our CORS configuration up to date.
That's it, your app should be available at `https://mycoolapp.com`!
#### Adding a `www` Subdomain
If you'd also like to access your app at `https://www.mycoolapp.com`, you can generate certificates for the `www` subdomain.
```shell
wasp deploy fly cmd --context client certs create www.mycoolapp.com
```
Once you do that, you will need to add another DNS record for your domain. It should be a CNAME record for `www` with the value of your root domain. Here's an example:
| Type | Name | Value | TTL |
| ----- | ---- | ------------- | ---- |
| CNAME | www | mycoolapp.com | 3600 |
With the CNAME record (Canonical name), you are assigning the `www` subdomain as an alias to the root domain.
Your app should now be available both at the root domain `https://mycoolapp.com` and the `www` sub-domain `https://www.mycoolapp.com`.
:::caution[CORS Configuration]
Using the `www` and `non-www` domains at the same time will require you to update your CORS configuration to allow both domains. You'll need to provide [custom CORS configuration](https://gist.github.com/infomiho/5ca98e5e2161df4ea78f76fc858d3ca2) in your server app to allow requests from both domains.
:::
### Environment Variables {#flyio-cli-environment-variables}
#### Server Secrets
If your app requires any other server-side environment variables (like social auth secrets), you can set them:
1. Initially, in the `launch` or `setup` commands with the [`--server-secret` option](#fly-launch-environment-variables)
2. After the app has already been deployed by using the `secrets set` command:
```
wasp deploy fly cmd secrets set GOOGLE_CLIENT_ID=<...> GOOGLE_CLIENT_SECRET=<...> --context=server
```
#### Client Environment Variables
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running a deployment command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy fly launch my-wasp-app dfw
```
or
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy fly deploy
```
Please note that you should do this for **every deployment**, not just the first time you set up the variables. One way to make sure you don't forget to add them is to create a `deploy` script in your `package.json` file:
```json title="package.json"
{
"scripts": {
"deploy": "REACT_APP_ANOTHER_VAR=somevalue wasp deploy fly deploy"
}
}
```
Then you can run `npm run deploy` to deploy your app.
### Fly.io Regions
> Fly.io runs applications physically close to users: in datacenters around the world, on servers we run ourselves. You can currently deploy your apps in 34 regions, connected to a global Anycast network that makes sure your users hit our nearest server, whether theyโre in Tokyo, Sรฃo Paolo, or Frankfurt.
Read more on Fly regions [here](https://fly.io/docs/reference/regions/).
You can find the list of all available Fly regions by running:
```shell
fly platform regions
```
### Multiple Fly.io Organizations
If you have multiple organizations, you can specify a `--org` option. For example:
```shell
wasp deploy fly launch my-wasp-app dfw --org hive
```
### Building Locally
Fly.io offers support for both **locally** built Docker containers and **remotely** built ones. However, for simplicity and reproducibility, the CLI defaults to the use of a remote Fly.io builder.
If you want to build locally, supply the `--build-locally` option to `wasp deploy fly launch` or `wasp deploy fly deploy`.
##### Using a custom PostgreSQL database
By default, Wasp uses the standard PostgreSQL Docker image provided by Fly.io when creating a new database for your app. However, if you have a need for a custom Docker image, e.g., your application requires specific PostgreSQL extensions (e.g., PostGIS), you can specify a Docker image with a custom PostgreSQL installation, with the `--db-image ` flag.
Your custom PostgreSQL image must be compatible with Fly.io, as their platform has some requirements to work properly. Since these requirements are not readily documented, an easy way to ensure compatibility is to base your custom image off the official Fly.io PostgreSQL image: [`flyio/postgres-flex`](https://hub.docker.com/r/flyio/postgres-flex).
We have crafted a small guide on [how to create a custom Docker image with PostGIS or pgvector for Fly.io](https://gist.github.com/cprecioso/e19e883138241c1a446f48d6187aae75). You can also use it as a starting point to create your own images with other extensions.
:::tip
You only need to specify the Docker image once, when first creating the app with any of these commands:
```shell
wasp deploy fly create-db --db-image
wasp deploy fly setup --db-image
wasp deploy fly launch --db-image
```
:::
### API Reference
#### `launch`
`launch` is a convenience command that runs `setup`, `create-db`, and `deploy` in sequence.
```shell
wasp deploy fly launch
```
It accepts the following arguments:
- `` required
The name of your app.
- `` required
The region where your app will be deployed. Read how to find the available regions [here](#flyio-regions).
Running `wasp deploy fly launch` is the same as running the following commands:
```shell
wasp deploy fly setup
wasp deploy fly create-db
wasp deploy fly deploy
```
##### Environment Variables {#fly-launch-environment-variables}
###### Server
If you are deploying an app that requires any other environment variables (like social auth secrets), you can set them with the `--server-secret` option:
```
wasp deploy fly launch my-wasp-app dfw --server-secret GOOGLE_CLIENT_ID=<...> --server-secret GOOGLE_CLIENT_SECRET=<...>
```
###### Client
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running the `launch` command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy fly launch my-wasp-app dfw
```
##### Custom Server URL
If you want your client to connect to a different server URL (for example, if you're using a custom domain for your server), use the `--custom-server-url` option:
```shell
wasp deploy fly launch my-wasp-app dfw --custom-server-url https://api.myapp.com
```
#### `setup`
The `setup` command registers your client and server apps on Fly, and sets up needed environment variables. It only needs to be run once, when initially creating the app. It does *not* trigger a deploy for the client or server apps.
```shell
wasp deploy fly setup
```
It accepts the following arguments:
- `` required
The name of your app.
- `` required
The region where your app will be deployed. Read how to find the available regions [here](#flyio-regions).
After running `setup`, Wasp creates two new files in your project root directory: `fly-server.toml` and `fly-client.toml`. You should include these files in your version control.
You **can edit the `fly-server.toml` and `fly-client.toml` files** to further configure your Fly deployments. Wasp will use the TOML files when you run `deploy`.
If you want to maintain multiple apps, you can add the `--fly-toml-dir ` option to point to different directories, like "dev" or "staging".
:::caution[Execute Only Once]
You should only run `setup` once per app. If you run it multiple times, it creates unnecessary apps on Fly.
:::
#### `create-db`
The `create-db` command creates a new database for your app.
```shell
wasp deploy fly create-db
```
It accepts the following arguments:
- `` required
The region where your app will be deployed. Read how to find the available regions [here](#flyio-regions).
:::caution[Execute Only Once]
You should only run `create-db` once per app. If you run it multiple times, it creates multiple databases, but your app needs only one.
:::
#### `deploy`
```shell
wasp deploy fly deploy
```
The `deploy` command pushes your built client and server live.
Run this command whenever you want to **update your deployed app** with the latest changes:
```shell
wasp deploy fly deploy
```
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running the `deploy` command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy fly deploy
```
You must specify your client-side environment variables every time you redeploy with the above command [to ensure they are included in the build process](https://wasp.sh/docs/deployment/env-vars#client-env-vars).
##### Custom Server URL {#custom-server-url}
If you want your client to connect to a different server URL (for example, if you're using a custom domain for your server), use the `--custom-server-url` option:
```shell
wasp deploy fly deploy --custom-server-url https://api.myapp.com
```
#### `cmd`
If you want to run arbitrary Fly commands (for example `fly secrets list` for your server app), here's how to do it:
```shell
wasp deploy fly cmd secrets list --context server
```
## Deployment / Deployment Methods / Wasp Deploy / Railway
[Railway](https://railway.com/?utm_medium=integration\&utm_source=docs\&utm_campaign=wasp) is a cloud development platform that streamlines building and deploying applications with built-in support for databases and services. It offers an intuitive interface and automates infrastructure.
### Prerequisites
To deploy to Railway using Wasp CLI:
1. Create a [Railway](https://railway.com/?utm_medium=integration\&utm_source=docs\&utm_campaign=wasp) account,
2. Install the [`railway` CLI](https://docs.railway.com/guides/cli?utm_medium=integration\&utm_source=docs\&utm_campaign=wasp#installing-the-cli) on your machine.
### Deploying
Using the Wasp CLI, you can easily deploy a new app to Railway with a single command:
```shell
wasp deploy railway launch my-wasp-app
```
Please do not CTRL-C or exit your terminal while the commands are running.
Keep in mind that:
1. Your project name (for example `my-wasp-app`) must be unique across all your Railway projects or deployment will fail (this is a current limitation of the Wasp CLI and Railway integration [#2926](https://github.com/wasp-lang/wasp/issues/2926)).
2. If you are a member of multiple Railway organizations, the CLI will prompt you to select the organization under which you want to deploy your app.
The project name is used as a base for your server and client service names on Railway:
- `my-wasp-app-client`
- `my-wasp-app-server`
Railway doesn't allow setting the database service name using the Railway CLI. It will always be named `Postgres`. This also applies when using the `--db-image` flag.
When you run the `launch` command, Wasp CLI knows how to connect different parts of your Wasp app together, so it sets up the required environment variables for your server app:
1. `WASP_WEB_CLIENT_URL` and `WASP_SERVER_URL` which are required to connect your client and server apps.
2. `DATABASE_URL` which is required to connect your server app to the database.
3. `JWT_SECRET` which is required for authentication to work.
If you have any additional environment variables that your app needs, read how to set them in the [API Reference](#railway-environment-variables) section.
If you want to automate the deployment process, check out the [CI/CD Deployment](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/ci-cd) page to re-deploy your app on each commit as part of your CI/CD pipeline.
### Using a Custom Domain For Your App {#custom-domain}
Setting up a custom domain is a three-step process:
1. Add your domain to the Railway client service:
- Go into the [Railway dashboard](https://railway.com/dashboard?utm_medium=integration\&utm_source=docs\&utm_campaign=wasp).
- Select your project (for example `my-wasp-app`).
- Click on the client service (for example `my-wasp-app-client`).
- Go to the **Settings** tab and click **Custom Domain**.
- Enter your domain name (for example `mycoolapp.com`) and port `8080`.
- Click **Add Domain**.
2. Update the DNS records for your domain, adding a CNAME record at the domain or subdomain you want, pointing to the address you've been given in the previous step. *This step depends on your domain provider, consult their documentation in case of doubt.*
3. To avoid [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) errors, you need to set your new client URL as the `WASP_WEB_CLIENT_URL` environment variable (for example `https://mycoolapp.com`) for your **server service** in the Railway dashboard.
- Go into the [Railway dashboard](https://railway.com/dashboard?utm_medium=integration\&utm_source=docs\&utm_campaign=wasp).
- Select your project (for example `my-wasp-app`).
- Click on the server service (for example `my-wasp-app-server`).
- Go to the **Variables** tab.
Update the `WASP_WEB_CLIENT_URL` variable with the new domain for your client.
That's it, your app should be available at `https://mycoolapp.com`!
### API Reference
#### The `launch` command
`launch` is a convenience command that runs `setup` and `deploy` in sequence.
```shell
wasp deploy railway launch
```
It accepts the following arguments:
- `` required
The name of your project.
Running `wasp deploy railway launch` is the same as running the following commands:
```shell
wasp deploy railway setup
wasp deploy railway deploy
```
##### Using a custom PostgreSQL database
By default, Wasp uses the standard PostgreSQL image provided by Railway when creating a new database for your app. However, if your application requires specific PostgreSQL extensions (e.g., PostGIS), you can specify a Docker image with a custom PostgreSQL installation, with the `--db-image ` flag.
:::tip
You only need to specify the Docker image once, when first creating the app.
:::
```shell
# Use PostGIS:
wasp deploy railway launch my-wasp-app --db-image postgis/postgis
```
```shell
# Use pgvector:
wasp deploy railway launch my-wasp-app --db-image pgvector/pgvector:pg16
```
The service name will always be `Postgres`, regardless of the image used.
##### Explicitly providing the Railway project ID
By default, Wasp CLI tries to create a new Railway project named ``. If you want to use an existing Railway project, pass its ID with `--existing-project-id` option:
```shell
wasp deploy railway launch --existing-project-id
```
##### Explicitly providing the Railway Workspace
By default, Wasp CLI will prompt you to select a Railway workspace for your project. If you want to skip the prompt and provide the workspace id or name directly, use the `--workspace` option:
```shell
wasp deploy railway launch --workspace
```
##### Environment Variables {#railway-launch-environment-variables}
###### Server
If you are deploying an app that requires any other environment variables (like social auth secrets), you can set them with the `--server-secret` option:
```
wasp deploy railway launch my-wasp-app --server-secret GOOGLE_CLIENT_ID=<...> --server-secret GOOGLE_CLIENT_SECRET=<...>
```
###### Client
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running the `launch` command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy railway launch my-wasp-app
```
##### Custom Server URL
If you want your client to connect to a different server URL (for example, if you're using a custom domain for your server), use the `--custom-server-url` option:
```shell
wasp deploy railway launch my-wasp-app --custom-server-url https://api.myapp.com
```
#### The `deploy` command
The `deploy` command deploys your client and server apps to Railway.
```shell
wasp deploy railway deploy
```
It accepts the following arguments:
- `` required
The name of your project.
Run this command whenever you want to **update your deployed app** with the latest changes:
```shell
wasp deploy railway deploy
```
##### Explicitly providing the Railway project ID
When you run the `deploy` command, Wasp CLI will use the Railway project that's linked to the Wasp project directory. If no Railway project is linked, the command will fail asking you to run the `setup` command first.
If you are deploying your Railway app in the CI, you can pass the `--existing-project-id` option to tell Wasp CLI the Railway project ID to use for the deployment:
```shell
wasp deploy railway deploy --existing-project-id
```
##### Other Available Options
- `--skip-client` - do not deploy the web client
- `--skip-server` - do not deploy the server
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running the `deploy` command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy railway deploy
```
You must specify your client-side environment variables every time you redeploy with the above command [to ensure they are included in the build process](https://wasp.sh/docs/deployment/env-vars#client-env-vars).
##### Custom Server URL {#custom-server-url}
If you want your client to connect to a different server URL (for example, if you're using a custom domain for your server), use the `--custom-server-url` option:
```shell
wasp deploy railway deploy my-wasp-app --custom-server-url https://api.myapp.com
```
#### The `setup` command
The `setup` command creates your client, server, and database services on Railway. It also configures environment variables. It does *not* deploy the client or server services.
```shell
wasp deploy railway setup
```
It accepts the following arguments:
- ``
the name of your project.
The project name is used as a base for your server and client service names on Railway:
- `-client`
- `-server`
Railway also creates a PostgreSQL database service named `Postgres`.
##### Using a custom PostgreSQL database {#using-a-custom-postgresql-database}
By default, Wasp uses the standard PostgreSQL image provided by Railway when creating a new database for your app. However, if your application requires specific PostgreSQL extensions (e.g., PostGIS), you can specify a Docker image with a custom PostgreSQL installation, with the `--db-image ` flag.
:::tip
You only need to specify the Docker image once, when first creating the app.
:::
```shell
# Use PostGIS:
wasp deploy railway setup my-wasp-app --db-image postgis/postgis
```
```shell
# Use pgvector:
wasp deploy railway setup my-wasp-app --db-image pgvector/pgvector:pg16
```
The service name will always be `Postgres`, regardless of the image used.
##### Explicitly providing the Railway project ID
By default, Wasp CLI tries to create a new Railway project named ``. If you want to use an existing Railway project, pass its ID with `--existing-project-id` option:
```shell
wasp deploy railway setup --existing-project-id
```
##### Explicitly providing the Railway Workspace
By default, Wasp CLI will prompt you to select in which Railway workspace you want to create your project. If you want to skip the prompt and provide the workspace id or name directly, use the `--workspace` option:
```shell
wasp deploy railway setup --workspace
```
:::caution[Execute Only Once]
You should only run `setup` once per app. Wasp CLI skips creating the services if they already exist.
:::
#### Environment Variables {#railway-environment-variables}
##### Server Secrets
If your app requires any other server-side environment variables (like social auth secrets), you can set them:
1. Initially in the `launch` or `setup` commands with the [`--server-secret` option](#railway-launch-environment-variables)
2. After the app has already been deployed, go into the Railway dashboard and set them in the **Variables** tab of your server service.
##### Client Environment Variables
If you've added any [client-side environment variables](https://wasp.sh/docs/project/env-vars#client-env-vars) to your app, pass them to the terminal session before running a deployment command, for example:
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy railway launch my-wasp-app
```
or
```shell
REACT_APP_ANOTHER_VAR=somevalue wasp deploy railway deploy
```
Please note that you should do this for **every deployment**, not just the first time you set up the variables. One way to make sure you don't forget to add them is to create a `deploy` script in your `package.json` file:
```json title="package.json"
{
"scripts": {
"deploy": "REACT_APP_ANOTHER_VAR=somevalue wasp deploy railway deploy"
}
}
```
Then you can run `npm run deploy` to deploy your app.
## Deployment / Deployment Methods / Wasp Deploy / CI/CD Deployment
You can use CI/CD platforms like Github Actions to re-deploy your application automatically whenever changes are pushed to your repository.
### Re-deploying from CI/CD
#### Prerequisites
Make sure to first deploy your application from your local machine using `wasp deploy launch`. The `launch` command creates services for your application in the deployment provider and deploys them. After your application is deployed, you are able to use `wasp deploy deploy` in a CI/CD workflow to re-deploy it.
#### [Fly.io ยป](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/fly)
#### [Railway ยป](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/railway)
Click on each provider for more details.
#### Deployment steps
To automate deployment, you need to create a workflow file in your repository that specifies the deployment process when a new commit is pushed to the repository.
The workflow needs to include the following steps:
1. Checkout the code from the repository
2. Install Node.js and the Wasp CLI
3. Install any provider-specific dependencies
4. Deploy the application using `wasp deploy deploy`
To be able to deploy your apps to the deployment providers, you need to set **provider-specific API keys** in the environment variables. How you set environment variables depends on the CI/CD platform you are using. We'll show you how to do this for [Github Actions](#github-actions-workflow) in the next section.
### Github Actions workflow
Let's take a look at an example CI/CD workflow for Github Actions for each of the supported providers.
**Fly.io**
You'll need an **organisation token** to deploy to Fly.io. You can generate the token by running [`fly tokens create org`](https://fly.io/docs/security/tokens/#create-org-scoped-tokens) and adding it to your repository secrets as `FLY_API_TOKEN`.
```yaml title=".github/workflows/deploy.yml"
name: Wasp Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
env:
WASP_VERSION: "0.25"
steps:
- uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: "24.14.1"
- name: Install Wasp
# We pin the Wasp CLI version to avoid issues when a new Wasp version is released.
run: npm i -g @wasp.sh/wasp-cli@$WASP_VERSION
- name: Install Flyctl
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy
run: wasp deploy fly deploy
env:
# You must add FLY_API_TOKEN to your Repository Secrets
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
```
**Railway**
You'll need an **account token** to deploy to Railway. You can generate it by going to your account settings under [Tokens](https://railway.com/account/tokens) and generating a token without selecting a workspace. Set the token as a repository secret named `RAILWAY_API_TOKEN`.
Make sure to replace `my-project-name` with the actual name of your project and `MY_PROJECT_ID` with the actual ID of your project. You can find the project ID in the project's settings.
```yaml title=".github/workflows/deploy.yml"
name: Wasp Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
env:
WASP_VERSION: "0.25"
RAILWAY_PROJECT_NAME: my-project-name
RAILWAY_PROJECT_ID: MY_PROJECT_ID
steps:
- uses: actions/checkout@v6
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: "24.14.1"
- name: Install Wasp
# We pin the Wasp CLI version to avoid issues when a new Wasp version is released.
run: npm i -g @wasp.sh/wasp-cli@$WASP_VERSION
- name: Install Railway CLI
run: npm install -g @railway/cli
- name: Deploy
run: wasp deploy railway deploy $RAILWAY_PROJECT_NAME --existing-project-id $RAILWAY_PROJECT_ID
env:
# You must add RAILWAY_API_TOKEN to your Repository Secrets
RAILWAY_API_TOKEN: ${{ secrets.RAILWAY_API_TOKEN }}
```
## Deployment / Deployment Methods / Cloud Providers
You can deploy the built Wasp app wherever and however you want, as long as your provider/server supports running a Node.js server, serving static files, and running a PostgreSQL database.
### Guides
We have step-by-step guides for deploying your Wasp app to some of the most popular providers you can follow:
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/cloudflare)
#### [Deploying Wasp to Cloudflare Workers ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/cloudflare)
[Uses Cloudflare Workers, Wrangler CLI](https://wasp.sh/docs/guides/deployment/cloud-providers/cloudflare)
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/flyio)
#### [Deploying Wasp to Fly.io ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/flyio)
[Uses Fly.io, fly CLI, Docker](https://wasp.sh/docs/guides/deployment/cloud-providers/flyio)
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/heroku)
#### [Deploying Wasp to Heroku ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/heroku)
[Uses Heroku, heroku CLI, Docker](https://wasp.sh/docs/guides/deployment/cloud-providers/heroku)
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/netlify)
#### [Deploying Wasp to Netlify ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/netlify)
[Uses Netlify, Netlify CLI](https://wasp.sh/docs/guides/deployment/cloud-providers/netlify)
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/railway)
#### [Deploying Wasp to Railway ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/railway)
[Uses Railway, Railway CLI](https://wasp.sh/docs/guides/deployment/cloud-providers/railway)
[Guide](https://wasp.sh/docs/guides/deployment/cloud-providers/render)
#### [Deploying Wasp on Render ยป](https://wasp.sh/docs/guides/deployment/cloud-providers/render)
[Uses Render, Blueprint (IaC)](https://wasp.sh/docs/guides/deployment/cloud-providers/render)
If your desired provider isn't on the list, no worries, you can still deploy your app - it just means we don't yet have a step-by-step guide for you to follow. Feel free to [open a PR](https://github.com/wasp-lang/wasp/new/release/web/docs/guides/deployment/cloud-providers) if you'd like to write one yourself :)
### Manual deployment
Deploying a Wasp app comes down to the following:
1. Generating deployable code.
2. Deploying the API server (backend).
3. Deploying the web client (frontend).
4. Deploying a PostgreSQL database and keeping it running.
Let's go through each of these steps.
#### 1. Generating Deployable Code
Running the command `wasp build` generates deployable code for the whole app in the `.wasp/out/` directory.
```
wasp build
```
:::caution[PostgreSQL in production]
You won't be able to build the app if you are using SQLite as a database (which is the default database). You'll have to [switch to PostgreSQL](https://wasp.sh/docs/data-model/databases#migrating-from-sqlite-to-postgresql) before deploying to production.
:::
#### 2. Deploying the API Server
There's a Dockerfile that defines an image for building the server in the `.wasp/out` directory.
To run the server in production, deploy this Docker image to a hosting provider and make sure the required env variables are correctly set up. Usually, you use the provider's dashboard UI or a CLI tool to set up these env variables.
Check the [required server env variables](https://wasp.sh/docs/deployment/env-vars#server-env-vars) and make sure they are set up for your server.
While these are the general instructions on deploying the server anywhere, we also have more detailed instructions for chosen providers below, so check that out for more guidance if you are deploying to one of those providers.
#### 3. Deploying the Web Client
To build the web app, run the following command from your project root:
```
REACT_APP_API_URL= npx vite build
```
where `` is the URL of the Wasp server that you previously deployed.
The build output will be in `.wasp/out/web-app/build`.
:::caution[Client Env Variables]
Remember, if you have defined any other [client-side env variables](https://wasp.sh/docs/project/env-vars#defining-env-vars-in-development) in your project, make sure to add them to the command above when [building your client](https://wasp.sh/docs/deployment/env-vars#client-env-vars)
:::
The command above will build the web client and put it in the `.wasp/out/web-app/build` directory, including the `200.html` file at the root that acts as the SPA fallback.
Since the result of building is just a bunch of static files, you can now deploy your web client to any static hosting provider (e.g. Netlify, Cloudflare, ...) by deploying the contents of `.wasp/out/web-app/build/`.
#### 4. Deploying the Database
Any PostgreSQL database will do, as long as you provide the server with the correct `DATABASE_URL` env var and ensure that the database is accessible from the server.
## Deployment / Deployment Methods / Self-Hosted
If you have your server or rent out a server, you can self-host your Wasp apps. Self-hosting your apps gives you full control over your apps and their data. It can be more cost-effective than a cloud provider since you can deploy multiple apps on a single server. However, you'll need to manage the server yourself, which can be time-consuming and require some technical knowledge.
### Guides
We have step-by-step guides for deploying your Wasp app on your server with different methods. Check out the guides below:
[Guide](https://wasp.sh/docs/guides/deployment/self-hosted/caprover)
#### [Deploying Wasp with Caprover on your server ยป](https://wasp.sh/docs/guides/deployment/self-hosted/caprover)
[Uses Caprover, Github Actions, Github Container Registry](https://wasp.sh/docs/guides/deployment/self-hosted/caprover)
[Guide](https://wasp.sh/docs/guides/deployment/self-hosted/coolify)
#### [Deploying Wasp with Coolify on your server ยป](https://wasp.sh/docs/guides/deployment/self-hosted/coolify)
[Uses Coolify, Github Actions, Github Container Registry](https://wasp.sh/docs/guides/deployment/self-hosted/coolify)
[Guide](https://wasp.sh/docs/guides/deployment/self-hosted/vps)
#### [Deploying Wasp with Docker on your server ยป](https://wasp.sh/docs/guides/deployment/self-hosted/vps)
[Uses Ubuntu, Git, Caddy, Docker](https://wasp.sh/docs/guides/deployment/self-hosted/vps)
If your desired provider isn't on the list, no worries, you can still deploy your app - it just means we don't yet have a step-by-step guide for you to follow. Feel free to [open a PR](https://github.com/wasp-lang/wasp/new/release/web/docs/guides/deployment/self-hosted) if you'd like to write one yourself :)
### Manual deployment
We will show you a general overview of the architecture of a self-hosted Wasp app and the steps you need to take to deploy your app on your server. This is a more manual process than using the guides above, but it gives you more control over your deployment and you'll learn how everything works. If you are looking for a more guided deployment, check out the guides above.
#### What you'll need
To successfully self-host your Wasp app, you need to have the following:
- A server with a public IP address. There are many cloud providers you can use to rent a server. Some popular ones are [AWS](https://aws.amazon.com/ec2/), [DigitalOcean](https://www.digitalocean.com/), [OVH](https://www.ovhcloud.com/en/vps/), and [Hetzner](https://www.hetzner.com/cloud/).
- A domain name, for example, `myapp.com` (needed for HTTPS support).
#### Architecture
To self-host your Wasp app, you'll follow these general steps:
1. From your **app's code**, let Wasp build a **server app** and a **client app**.
2. Set up the **server environment variables** on the server.
3. Run a **database** on the server or use a managed database service.
4. Run the **server app** on the server, with or without Docker.
5. Serve the **client app** with a static file server.
6. Set up a **reverse proxy** on the server to be able to use a domain name with HTTPS for your app.

One possible self-hosting setup
#### Steps
1. Install [Docker](https://docs.docker.com/engine/install/), [Node.js](https://github.com/nvm-sh/nvm) and [Wasp CLI](https://wasp.sh/docs/quick-start#installation).
2. Get your **app's source code**.
- We recommend using Git to clone your app's repository and then pulling the latest changes when you want to deploy a new version. You can use any other method to get your app's code on the server.
3. Install dependencies with **`wasp install`** and build your app with **`wasp build`**.
4. Build and run the **server app**.
- Wasp gives you a `Dockerfile` in the `.wasp/out` directory that you can use to build and run the server app.
- We are using Docker to run the server app, but you can run it without Docker if you prefer - just make sure to replicate the setup in the `Dockerfile`.
- When you run the server app with Docker, you need to setup the server env variables. You can do this with a `.env` file or by passing the env variables directly to the `docker run` command.
5. Start the **database** on the server or use a managed database service.
- We usually run the database in Docker on the same server, but you can run the database directly on the server.
- You can also use a managed database service which you can connect to from your server. This is a great option if you don't want to manage the database yourself, but it can be more expensive.
6. Build the **client app** into static files.
- Wasp outputs the client app in the `.wasp/out/web-app` directory.
* You should [build the client app](https://wasp.sh/docs/deployment/deployment-methods/cloud-providers#3-deploying-the-web-client) into static files.
7. Install and set up a **reverse proxy** to serve your client and server apps.
- There are many great choices for reverse proxies, like [Nginx](https://www.nginx.com/), [Caddy](https://caddyserver.com/), and [Traefik](https://traefik.io/).
- Make sure to set up the reverse proxy to serve the client app's static files and to proxy requests to the server app.
8. Point your **domain(s)** to your server's IP address.
- We recommend setting `myapp.com` for the client and `api.myapp.com` for the server.
- The reverse proxy should serve the client app on `myapp.com` and proxy requests to the server app on `api.myapp.com`. Make sure your [env variables](https://wasp.sh/docs/deployment/env-vars) are using these client and server URLs.
### Database setup
By default, our self-hosted deployment methods run the **database on your server**. When you run the database on your server, you need to take care of backups, updates, and scaling. We suggest setting up [PostgresSQL periodic backups](https://tembo.io/docs/getting-started/postgres_guides/how-to-backup-and-restore-a-postgres-database) and/or taking snapshots of your server's disk. In case something bad happens to your server, you can restore your database from the backups.
If you prefer not to manage the database yourself, you can use a **managed database service**. The service provider takes care of backups, updates, and scaling for you but it can be more expensive than running the database on your server. Some popular managed database services are [AWS RDS](https://aws.amazon.com/rds/), [DigitalOcean Managed Databases](https://www.digitalocean.com/products/managed-databases/), and [Supabase](https://supabase.io/).
## Deployment / CI/CD Overview
Setting up a CI/CD pipeline is an optional but highly recommended part of deploying applications.
**Continuous Integration (CI)** involves verifying/testing code changes through an automated process whenever code is pushed to the repository. This helps us catch bugs early and make sure that our app works.
**Continuous Deployment (CD)** refers to the automatic deployment of code changes to the production environment. This is commonly know as "push to deploy" and frees developers from having to manually deploy code changes.
### Running tests in CI
#### End to end tests
End to end (e2e) tests simulate real user using your app and you can test different scenarios like login, adding items to cart, etc. Writing end to end tests frees you from manually testing your app after every change.
**To run e2e tests with Wasp in the CI**, you'll need to:
1. Install Wasp in the CI environment.
2. Run your app (with the database) in the CI environment.
3. Run the e2e tests against the running app.
##### Example app
We'll show you how to run end-to-end tests in CI using the [Github Actions](https://github.com/features/actions) as our CI and the [Playwright](https://playwright.dev/) as our e2e testing framework.
1. Check our example app and its e2e tests in the [e2e-tests](https://github.com/wasp-lang/e2e-test-example/tree/main/e2e-tests) directory.
You can copy the `e2e-tests` directory to your own project and modify it to fit your app. This will enable you to run the e2e tests locally.
Example e2e test
```ts
import { expect, test } from '@playwright/test'
import { generateRandomUser, logUserIn } from './utils'
const user = generateRandomUser()
test.describe('basic user flow test', () => {
test('log in and add task', async ({ page }) => {
await logUserIn({ page, user })
await expect(page).toHaveURL('/')
await expect(page.locator('body')).toContainText('No tasks yet.')
// Add a task
await page.fill('input[name="description"]', 'First task')
await page.click('input:has-text("Create task")')
await expect(page.locator('body')).toContainText('First task')
})
})
```
2. To run the tests in the Github Actions CI, you'll need to create a workflow file in your repository.
You should create a `.github/workflows/e2e-tests.yml` file in your repository. You can copy the contents of the [e2e-tests.yml](https://github.com/wasp-lang/e2e-test-example/blob/main/.github/workflows/e2e-tests.yml) file from our example app.
#### Unit tests
Unit tests test pieces of your code logic in isolation. They are much simpler and faster than e2e tests, but they don't simulate the real user interaction with your app.
You can use Wasp's built in [client tests](https://wasp.sh/docs/project/testing) support to test the client side code of your app. You are free to use any testing framework for the server side code.
**You'd run the unit tests in the CI** in a similar way as the e2e tests:
1. Install Wasp in the CI environment.
2. Run the client tests with `wasp test client run`.
3. Run the server tests with your testing framework.
### Continuous deployment
We'll look at two ways you can use the CI/CD pipeline to deploy your Wasp app:
1. Package the server and client with Docker.
2. Deploy the client as static files.
#### Package the server and client with Docker
The most common way to package your app for deployment is using Docker images. This way you can easily deploy the same image to different environments (staging, production, etc.).
**To build the app as a Docker image**, you'll need to:
1. Install Docker in the CD environment.
2. Install dependencies with `wasp install` and build the app with `wasp build`.
3. Build the Docker image and push it to a Docker registry:
- for our server app
- for our client app
4. For some providers: notify them to deploy the new app version.
:::info[What is a Docker Registry?]
Docker Registry is a place where you can store your Docker images and then your deployment provider can pull them from there. The most common Docker Registry is the [Docker Hub](https://hub.docker.com/), but you can also use other registries like the [Github Container Registry (GHCR)](https://docs.github.com/en/packages/guides/about-github-container-registry).
:::
##### Example deployment
We'll take a look at our Coolify deployment example in the [deployment](https://wasp.sh/docs/guides/deployment/self-hosted/coolify) section. We are using Github Actions to build the Docker images and their Github Container Registry (GHCR) to store them.
Let's go through the [deploy.yml](https://gist.github.com/infomiho/ad6fade7396498ae32a931ca563a4524#file-deploy-yml) file in the Coolify guide:
1. First, we **authenticate with the Github Container Registry (GHCR)**.
We are using the `docker/login-action` action to authenticate with the GHCR.
2. Then, we **prepare the Docker image metadata** for later use.
We are using the `docker/metadata-action` action to prepare some extra info that we'll use later in the deployment process.
3. Next, we **install dependencies** with `wasp install` and **build the Wasp app** with `wasp build`.
This creates our server and the client app in the `.wasp/out` folder.
4. Then, we **package the server app** into a Docker image and **push it to the GHCR**.
We use the `Dockerfile` in the `.wasp/out` directory to build and push the server Docker image using the `docker/build-push-action` action.
5. Next, we create a `Dockerfile` for our client and then **package the client app** into a Docker image and **push it to the GHCR**.
We create a `Dockerfile` that uses a simple Go static server to serve the client app. We again use the `docker/build-push-action` action to build and push the client Docker image.
6. Finally, we notify Coolify using their Webhook API to **deploy our new app version**.
And now you can open the [deploy.yml](https://gist.github.com/infomiho/ad6fade7396498ae32a931ca563a4524#file-deploy-yml) file in the Coolify guide and see the full deployment process.
#### Static build of the client
Wasp's client app is a single page application (SPA) which you build into static HTML, CSS, and JS files that you can upload to any hosting provider that supports serving static files. This means that for the client app, you don't need to use Docker images if don't want to. It's usually cheaper to host static files than to host Docker images.
**To deploy the client app as static files**, you'll need to:
1. Install dependencies with `wasp install` and build the app with `wasp build` in the CD environment.
2. Build the client app with `npx vite build`.
3. Upload the static files (from `.wasp/out/web-app/build`) to your hosting provider.
Check out our instructions for deploying the client app to [Netlify](https://wasp.sh/docs/guides/deployment/cloud-providers/netlify) or [Cloudflare](https://wasp.sh/docs/guides/deployment/cloud-providers/cloudflare) where you can check out the example deployment using Github Actions.
## Deployment / Extras
In this section, we will cover some additional topics that are important for deploying Wasp apps in production.
#### Custom domain setup
If you want to set up a custom domain for your Wasp app, you can do it for both the client and the server.
The important part is setting up the custom domain for the client - that's what your users visit from their browsers. Setting up a custom domain for the server is optional, but it can be useful if you'd like to hide some server details (for example, the IP address or auto-generated domain name) from the users.
##### How to do it?
It's usually a two-step process, and it's the same for both the client and the server:
1. Set up the **DNS records** for the domain.
This will depend on your hosting provider. You can usually do this by adding an `A` record in your DNS settings that points to the app's IPv4 address. You often set the `AAAA` record for IPv6 address as well. Some hosting providers ask you to set the `CNAME` record instead of the `A` and `AAAA` records.
:::note[Using wasp deploy?]
Check out how to set up custom domains with [Fly.io](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/fly#custom-domain) or [Railway](https://wasp.sh/docs/deployment/deployment-methods/wasp-deploy/railway#custom-domain).
:::
2. Set up the **environment variables** for the app.
You need to set the environment variables so Wasp configures the app correctly (for example, for CORS to work correctly).
##### Client domain env vars
When [building the client](https://wasp.sh/docs/deployment/env-vars#client-env-vars), set `REACT_APP_API_URL` to point to your server domain:
```bash
REACT_APP_API_URL=https://api.myapp.com
```
Learn more about client configuration in the [env vars section](https://wasp.sh/docs/project/env-vars#client-general-configuration).
##### Server domain env vars
For the server, you need to [configure two variables](https://wasp.sh/docs/deployment/env-vars#server-env-vars):
- `WASP_WEB_CLIENT_URL`: Your client app's domain
- `WASP_SERVER_URL`: Your server domain
```bash
WASP_WEB_CLIENT_URL=https://myapp.com
WASP_SERVER_URL=https://server.myapp.com
```
Learn more about server env variables in the [env vars section](https://wasp.sh/docs/project/env-vars#server-general-configuration).
#### DDoS protection and CDN recommendations
When deploying your Wasp app, you might want to consider using a Content Delivery Network (CDN) and DDoS protection service to improve the performance and security of your app:
1. **Content Delivery Network (CDN)** is a network of servers distributed worldwide that caches static assets like images, CSS, and JavaScript files.
Using a CDN in front of your **client** can help with caching static assets and serving them faster to users around the world. When a user requests a file, the CDN serves it from the server closest to the user, improving load times.
2. **Distributed Denial of Service (DDoS)** attacks are a common threat to web applications.
Attackers send a large amount of traffic to your server, overwhelming it and making it unavailable to legitimate users. You can use a DDoS protection service for both your **client and server** to protect your app from these attacks.
We recommend using [Cloudflare](https://www.cloudflare.com/) for both CDN and DDoS protection. It's easy to set up and provides a free tier that should be enough for most small to medium-sized apps.
There are other CDN providers like [Fastly](https://www.fastly.com/), [Bunny](https://bunnycdn.com/) and [Amazon Cloudfront](https://aws.amazon.com/cloudfront/) that you can consider as well.
#### Are Wasp apps production ready?
As we mentioned in the [introduction](https://wasp.sh/docs/deployment/intro) section, what we call **Wasp apps** are three separate pieces: the client, the server, and the database.
For the server, we are using Node.js and the battle-tested Express.js framework. For the database, we are using PostgreSQL, which is a powerful and reliable database system. For the client, we are using React and Vite, which are both widely used and well-maintained.
Each of these pieces is production-ready on its own, and Wasp just makes it easy to connect them together. Keep in mind that Wasp is still considered beta software, so there might be some rough edges here and there.
## AI & Coding Agents / Agent Plugin / Skills
Wasp provides an official plugin for coding agents that transforms them into Wasp framework experts.
The plugin gives your agent curated access to Wasp docs, workflows, and best practices so it can develop full-stack web apps (React, Node.js, Prisma) more effectively.
The plugin / skills work with just about all the popular coding agents, such as [Claude Code](https://claude.com/product/claude-code), [Cursor](https://www.cursor.com/), [Codex](https://openai.com/codex/), [Gemini CLI](https://geminicli.com/), [GitHub Copilot](https://github.com/features/copilot/cli), [OpenCode](https://opencode.ai/), and more.
### Features
- **Wasp Documentation** โ Ensures your agent always accesses LLM-friendly Wasp docs in sync with your current project's Wasp version.
- **Wasp Knowledge** โ Imports Wasp best practices and conventions into your project's memory file (e.g. `CLAUDE.md`, `AGENTS.md`).
- **Feature Configuration** โ Easily add Wasp features like authentication, database, email, styling (Tailwind, shadcn/ui), and other integrations through your agent.
- **Deployment Guidance** โ Your agent will guide you through deploying your Wasp app to Railway or Fly.io via the Wasp CLI, or manually to your favorite cloud provider.
### Installation
#### Claude Code
First, add the Wasp plugin marketplace:
```bash
claude plugin marketplace add wasp-lang/wasp-agent-plugins
```
Then install the Wasp plugin:
```bash
claude plugin install wasp@wasp-agent-plugins --scope project
```
:::tip
We recommend installing with `project` scope so the settings are committed to git (via `settings.json`). Use `local` scope if you prefer settings that aren't committed (via `settings.local.json`).
:::
#### Other Agents (Cursor, Codex, Gemini, Copilot, OpenCode, etc.)
Run the following command and select all the skills:
```bash
npx skills add wasp-lang/wasp-agent-plugins
```
### Setup
After installing, initialize the plugin in an active agent session by explicitly invoking the `/wasp-plugin-init` skill:
```
Run the '/wasp-plugin-init' skill.
```
This adds Wasp knowledge to your project's `CLAUDE.md` or `AGENTS.md` file.
Next, start the development server as a background task so your agent has full insight into the running app while developing:
```
Run the 'start-dev-server' skill.
```
To see all available features and skills:
```
/wasp-plugin-help
```
### Learn More
Check out the [Wasp Agent Plugins repository](https://github.com/wasp-lang/wasp-agent-plugins) for more details.
## Advanced Features / Sending Emails
With Wasp's email-sending feature, you can easily integrate email functionality into your web application.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "",
defaultFrom: {
name: "Example",
email: "hello@itsme.com",
},
},
// ...
})
```
Choose from one of the providers:
- `Dummy` (development only),
- `Mailgun`,
- `SendGrid`,
- `Resend`
- or the good old `SMTP`.
Optionally, define the `defaultFrom` field, so you don't need to provide it whenever sending an email.
### Sending Emails {#sending-emails-1}
Before jumping into details about setting up various providers, let's see how easy it is to send emails.
You import the `emailSender` that is provided by the `wasp/server/email` module and call the `send` method on it.
```ts title="src/actions/sendEmail.ts"
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello world",
});
```
Read more about the `send` method in the [API Reference](#javascript-api).
The `send` method returns an object with the status of the sent email. It varies depending on the provider you use.
### Providers
We'll go over all of the available providers in the next section. For some of them, you'll need to set up some env variables. You can do that in the `.env.server` file.
#### Using the Dummy Provider {#dummy}
:::note[Dummy Provider is not for production use]
The `Dummy` provider is not for production use. It is only meant to be used during development. If you try building your app with the `Dummy` provider, the build will fail.
:::
To speed up development, Wasp offers a `Dummy` email sender that `console.log`s the emails in the console. Since it doesn't send emails for real, it doesn't require any setup.
Set the provider to `Dummy` in your `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "Dummy",
},
// ...
})
```
#### Using the SMTP Provider {#smtp}
First, set the provider to `SMTP` in your `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "SMTP",
},
// ...
})
```
Then, add the following env variables to your `.env.server` file.
```properties title=".env.server"
SMTP_HOST=
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_PORT=
```
Many transactional email providers (e.g. Mailgun, SendGrid but also others) can also use SMTP, so you can use them as well.
:::caution[SMTP ports might be blocked]
Some hosting providers (for example, **Railway** on its free tier, or **Hetzner**) block outbound SMTP ports to prevent spam. If you run into issues, check their documentation for a solution, or consider using a dedicated provider integration like [Mailgun](#mailgun) or [SendGrid](#sendgrid) instead of plain SMTP.
:::
#### Using the Mailgun Provider {#mailgun}
Set the provider to `Mailgun` in the `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "Mailgun",
},
// ...
})
```
Then, get the Mailgun API key and domain and add them to your `.env.server` file.
##### Getting the API Key and Domain
1. Go to [Mailgun](https://www.mailgun.com/) and create an account.
2. Go to [Domains](https://app.mailgun.com/mg/sending/new-domain) and create a new domain.
3. Copy the domain and add it to your `.env.server` file.
4. Create a new Sending API key under `Send > Sending > Domain settings` and find `Sending API keys`.
5. Copy the API key and add it to your `.env.server` file.
```properties title=".env.server"
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
```
##### Using the EU Region
If your domain region is in the EU, you need to set the `MAILGUN_API_URL` variable in your `.env.server` file:
```properties title=".env.server"
MAILGUN_API_URL=https://api.eu.mailgun.net
```
#### Using the SendGrid Provider {#sendgrid}
:::caution[SendGrid Free Plan Retired]
As of May 27, 2025, SendGrid has [retired its free plans](https://www.twilio.com/en-us/changelog/sendgrid-free-plan). A paid SendGrid plan is now required to send emails. Consider using [Mailgun](#mailgun) or [SMTP](#smtp) with another provider if you need a free tier option.
:::
Set the provider field to `SendGrid` in your `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "SendGrid",
},
// ...
})
```
Then, get the SendGrid API key and add it to your `.env.server` file.
##### Getting the API Key
1. Go to [SendGrid](https://sendgrid.com/) and create an account (paid plan required).
2. Go to [API Keys](https://app.sendgrid.com/settings/api_keys) and create a new API key.
3. Copy the API key and add it to your `.env.server` file.
```properties title=".env.server"
SENDGRID_API_KEY=
```
#### Using the Resend Provider {#resend}
Set the provider field to `Resend` in your `main.wasp.ts` file.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "myApp",
emailSender: {
provider: "Resend",
},
// ...
})
```
Then, get the Resend API key and add it to your `.env.server` file.
##### Getting the API Key
1. Go to [Resend](https://resend.com/) and create an account.
2. Go to [API Keys](https://resend.com/api-keys) and create a new API key.
3. Copy the API key and add it to your `.env.server` file.
```properties title=".env.server"
RESEND_API_KEY=
```
### API Reference
#### `emailSender` specification
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailSender)
#### [EmailSender ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailSender)
[All the options for the emailSender field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/EmailSender)
#### JavaScript API
Using the `emailSender` in Typescript:
```ts title="src/actions/sendEmail.ts"
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello world",
});
```
The `send` method accepts an object with the following fields:
- `from: object`
The sender's details. If you set up the `defaultFrom` field in the `emailSender` config in your Wasp file, this field is optional.
- `name: string`
The name of the sender.
- `email: string`
The email address of the sender.
- `to: string` required
The recipient's email address.
- `subject: string` required
The subject of the email.
- `text: string` required
The text version of the email.
- `html: string` required
The HTML version of the email
## Advanced Features / Recurring Jobs
In most web apps, users send requests to the server and receive responses with some data. When the server responds quickly, the app feels responsive and smooth.
What if the server needs extra time to fully process the request? This might mean sending an email or making a slow HTTP request to an external API. In that case, it's a good idea to respond to the user as soon as possible and do the remaining work in the background.
Wasp supports background jobs that can help you with this:
- Jobs persist between server restarts,
- Jobs can be retried if they fail,
- Jobs can be delayed until a future time,
- Jobs can have a recurring schedule.
### Using Jobs
#### Job spec and Usage
Let's write an example Job that will print a message to the console and return a list of tasks from the database.
1. Start by creating a Job spec in your Wasp file:
```ts title="main.wasp.ts"
import { app, job } from "@wasp.sh/spec"
import { mySpecialJob } from "./src/workers/bar" with { type: "ref" }
export default app({
// ...
spec: [
job(mySpecialJob, {
executor: "PgBoss",
entities: ["Task"],
}),
],
})
```
:::note
When `main.wasp.ts` needs to point to your code, it uses imports like this:
```ts
import { MainPage } from "./src/MainPage" with { type: "ref" }
```
Notice the `with { type: "ref" }` part at the end of the import statement. This tells Wasp to treat the import as a reference to your app's code, without running the imported code. For more details and examples, see [reference imports](https://wasp.sh/docs/general/spec#reference-imports).
:::
2. After declaring the Job, implement its worker function:
```ts title="src/workers/bar.ts"
import { type MySpecialJob } from "wasp/server/jobs"
import { type Task } from "wasp/entities"
type Input = { name: string; }
type Output = { tasks: Task[]; }
export const mySpecialJob: MySpecialJob = async ({ name }, context) => {
console.log(`Hello ${name}!`)
const tasks = await context.entities.Task.findMany({})
return { tasks }
}
```
:::info[The worker function]
The worker function must be an `async` function. The function's return value represents the Job's result.
The worker function accepts two arguments:
- `args`: The data passed into the job when it's submitted.
- `context: { entities }`: The context object containing entities you put in the Job spec.
:::
`MySpecialJob` is a generic type Wasp generates to help you correctly type the Job's worker function, ensuring type information about the function's arguments and return value. Read more about type-safe jobs in the [JavaScript API section](#javascript-api).
3. After successfully defining the job, you can submit work to be done in your [Operations](https://wasp.sh/docs/data-model/operations/overview) or [setupFn](https://wasp.sh/docs/project/server-config#setup-function) (or any other NodeJS code):
```ts title="someAction.ts"
import { mySpecialJob } from "wasp/server/jobs"
const submittedJob = await mySpecialJob.submit({ name: "Johnny" })
// Or, if you'd prefer it to execute in the future, just add a .delay().
// It takes a number of seconds, Date, or ISO date string.
await mySpecialJob
.delay(10)
.submit({ name: "Johnny" })
```
And that's it. Your job will be executed by `PgBoss` as if you called `mySpecialJob({ name: "Johnny" })`.
In our example, `mySpecialJob` takes an argument, but passing arguments to jobs is not a requirement. It depends on how you've implemented your worker function.
#### Recurring Jobs
If you have work that needs to be done on some recurring basis, you can add a `schedule` to your job spec:
```ts title="main.wasp.ts"
import { app, job } from "@wasp.sh/spec"
import { mySpecialJob } from "./src/workers/bar" with { type: "ref" }
export default app({
// ...
spec: [
job(mySpecialJob, {
executor: "PgBoss",
schedule: {
cron: "0 * * * *",
args: { name: "Johnny" }, // optional
},
}),
],
})
```
In this example, you *don't* need to invoke anything in TypeScript. You can imagine `mySpecialJob({ name: "Johnny" })` getting automatically scheduled and invoked for you every hour.
### Job executors
Wasp supports Jobs through the use of **job executors**. A job executor is responsible for handling the scheduling, monitoring, and execution of jobs.
Currently, Wasp only has support for one job executor, `PgBoss`.
#### `PgBoss`
[`PgBoss`](https://github.com/timgit/pg-boss/tree/8.4.2) is a lightweight job queue built on top of PostgreSQL. It is suitable for low-volume production use cases and does not require any additional infrastructure or complex management. By using PostgreSQL (and [SKIP LOCKED](https://www.2ndquadrant.com/en/blog/what-is-select-skip-locked-for-in-postgresql-9-5/)) as its storage and synchronization mechanism, you get many benefits of a traditional job queue, on top of your existing Postgres database.
##### Requirements
`PgBoss` requires that your database provider is set to `"postgresql"` in your `schema.prisma` file. Read more about setting the provider [here](https://wasp.sh/docs/data-model/databases#postgresql).
##### Limitations
`PgBoss` runs together with your web server, whenever it is up. This means that it is not a separate process or service, but rather a part of your web server's application. As such, it is not suitable for CPU-heavy workloads, as it shares the CPU with your web server's application logic.
The `PgBoss` executor in Wasp does not (yet) support independent, horizontal scaling of pg-boss-only applications, nor starting them as separate workers/processes/threads. This means that your server must be running whenever you want to process jobs. If you need to scale your job processing, you will need to run multiple instances of your web server, each with its own `PgBoss` instance.
##### Customization {#pg\_boss\_new\_options}
If you need to customize the creation of the `PgBoss` instance, you can set an environment variable called `PG_BOSS_NEW_OPTIONS` to a stringified JSON object containing the initialization parameters. See the [pg-boss documentation](https://github.com/timgit/pg-boss/tree/8.4.2/docs#newoptions).
Please note that setting `PG_BOSS_NEW_OPTIONS` environment variable overwrites all Wasp defaults, so you must include the `connectionString` parameter inside it as well.
For example, to set the connection string and change the job archival and deletion settings, you can set the environment variable like this:
```bash
# In an .env file
PG_BOSS_NEW_OPTIONS={"connectionString":"postgresql://user:password@server:5432/database","archiveCompletedAfterSeconds":86400,"deleteAfterDays":30,"maintenanceIntervalMinutes":5}
# In the shell
PG_BOSS_NEW_OPTIONS='{"connectionString":"postgresql://user:password@server:5432/database","archiveCompletedAfterSeconds":86400,"deleteAfterDays":30,"maintenanceIntervalMinutes":5}'
```
You can read more about escaping JSON in environment variables in the [JSON Env Vars documentation](https://wasp.sh/docs/project/env-vars#json-env-vars).
##### Database setup
:::tip[You don't need to set up the database manually]
When using `PgBoss`, the database setup is automatically taken care of by the Wasp server, and doesn't need to be reflected in your schemas or migrations. The following information is given for your reference, and is explained in more detail in [the `PgBoss` documentation](https://github.com/timgit/pg-boss/blob/8.4.2/docs/readme.md).
:::
All job data will be stored in a separate database schema called `pgboss`. It has some internal tracking tables, such as `job`, `archive`, and `schedule`. `PgBoss` tables have a `name` column in most tables that will correspond to your Job identifier. Additionally, these tables maintain arguments, states, return values, retry information, start and expiration times, and other metadata required by `PgBoss`.
##### Known issues
- **Renaming scheduled jobs**
Wasp derives the Job's name from the worker function you pass to `job`. For example, `job(emailReminder, ...)` creates a Job named `emailReminder`, and Wasp uses that name in the `name` column of `pgboss` tables. If you change a name that had a `schedule` associated with it, pg-boss will continue scheduling those jobs but they will have no handlers associated, and will thus become stale and expire. To resolve this, you can remove the applicable row from the `pgboss.schedule` table.
For example, if you renamed a job from `emailReminder` to `sendEmailReminder`, you would need to remove the old scheduled job with the following SQL query:
```sql
BEGIN;
DELETE FROM pgboss.schedule WHERE name = 'emailReminder';
COMMIT;
```
**Important:** Only modify the database directly if you're comfortable with SQL operations. If you're unsure, consider keeping the old job name or restarting with a fresh database in development.
##### Job data retention and cleanup
By default, `PgBoss` keeps job data for 12 hours after completion or failure. After that, it moves the data to an archive table, where it is kept for 7 days before being deleted. If you want to change this behavior, you can configure the `PG_BOSS_NEW_OPTIONS` environment variable to set custom values for job archival ([`archivedCompletedAfterSeconds`/`archiveFailedAfterSeconds`](https://github.com/timgit/pg-boss/tree/8.4.2/docs#newoptions:~\:text=v1%22%20or%20%22v4%22-,archiveCompletedAfterSeconds,-Specifies%20how%20long)) and removal ([`deleteAfterSeconds`/`deleteAfterMinutes`/etc](https://github.com/timgit/pg-boss/tree/8.4.2/docs#newoptions:~\:text=the%20skew%20warnings.-,Archive%20options,-When%20jobs%20in)).
```bash
PG_BOSS_NEW_OPTIONS={"connectionString":"...your postgress connection url...","archiveCompletedAfterSeconds":86400,"deleteAfterDays":30,"maintenanceIntervalMinutes":5}
```
### API Reference
#### `job` specification
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/job)
#### [job ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/job)
[All the options for defining a job in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/job)
#### JavaScript API
##### The worker function {#worker-api}
An `async` function that performs the Job's work. Since Wasp executes Jobs on the server, its import path must lead to a NodeJS file. It receives two arguments:
- `args: Input`: The data passed to the job when it's submitted.
- `context: { entities: Entities }`: The context object containing the entities you put in the Job spec.
Here's an example worker function:
```ts title="src/workers/bar.ts"
import { type MySpecialJob } from "wasp/server/jobs"
type Input = { name: string; }
type Output = { tasks: Task[]; }
export const mySpecialJob: MySpecialJob = async ({ name }, context) => {
console.log(`Hello ${name}!`)
const tasks = await context.entities.Task.findMany({})
return { tasks }
}
```
Read more about type-safe jobs in the [JavaScript API section](#javascript-api).
##### Importing a Job:
```ts title="someAction.ts"
import { mySpecialJob, type MySpecialJob } from "wasp/server/jobs"
```
:::info[Type-safe jobs]
Wasp generates a generic type for each Job, which you can use to type your worker function. The type is named after the worker function you pass to `job`, converted to PascalCase, and is available in the `wasp/server/jobs` module. In the example above, the type is `MySpecialJob`.
The type takes two type arguments:
- `Input`: The type of the `args` argument of the worker function.
- `Output`: The type of the return value of the worker function.
:::
##### `submit(jobArgs, executorOptions)`
- `jobArgs: Input`
- `executorOptions: object`
Submits a Job to be executed by an executor, optionally passing in a JSON job argument your job handler function receives, and executor-specific submit options.
```ts title="someAction.ts"
const submittedJob = await mySpecialJob.submit({ name: "Johnny" })
```
##### `delay(startAfter)`
- `startAfter: int | string | Date` required
Delaying the invocation of the job handler. The delay can be one of:
- Integer: number of seconds to delay. \[Default 0]
- String: ISO date string to run at.
- Date: Date to run at.
```ts title="someAction.ts"
const submittedJob = await mySpecialJob
.delay(10)
.submit({ name: "Johnny" }, { "retryLimit": 2 })
```
##### Tracking
The return value of `submit()` is an instance of `SubmittedJob`, which has the following fields:
- `jobId`: The ID for the job in that executor.
- `jobName`: The Job name Wasp derived from the worker function you passed to `job`.
- `executorName`: The Symbol of the name of the job executor.
There are also some namespaced, job executor-specific objects.
- For pg-boss, you may access: `pgBoss`
- `details()`: pg-boss specific job detail information. [Reference](https://github.com/timgit/pg-boss/blob/8.4.2/docs/readme.md#getjobbyidid)
- `cancel()`: attempts to cancel a job. [Reference](https://github.com/timgit/pg-boss/blob/8.4.2/docs/readme.md#cancelid)
- `resume()`: attempts to resume a canceled job. [Reference](https://github.com/timgit/pg-boss/blob/8.4.2/docs/readme.md#resumeid)
## Advanced Features / Web Sockets
Wasp provides a fully integrated WebSocket experience by utilizing [Socket.IO](https://socket.io/) on the client and server.
We handle making sure your URLs are correctly setup, CORS is enabled, and provide a useful `useSocket` and `useSocketListener` abstractions for use in React components.
To get started, you need to:
1. Define your WebSocket logic on the server.
2. Enable WebSockets in your Wasp file, and connect it with your server logic.
3. Use WebSockets on the client, in React, via `useSocket` and `useSocketListener`.
4. Optionally, type the WebSocket events and payloads for full-stack type safety.
Let's go through setting up WebSockets step by step, starting with enabling WebSockets in your Wasp file.
### Turn On WebSockets in Your Wasp File
We specify that we are using WebSockets by adding `webSocket` to our `app` and providing the required `fn`. You can optionally change the auto-connect behavior.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { webSocketFn } from "./src/webSocket" with { type: "ref" }
export default app({
name: "myApp",
webSocket: {
fn: webSocketFn,
autoConnect: true, // optional, default: true
},
// ...
})
```
### Defining the Events Handler
Let's define the WebSockets server with all of the events and handler functions.
:::info[Full-stack type safety]
Check this out: we'll define the event types and payloads on the server, and they will be **automatically exposed on the client**. This helps you avoid mistakes when emitting events or handling them.
:::
#### `webSocketFn` Function {#websocketfn}
On the server, you will get Socket.IO `io: Server` argument and `context` for your WebSocket function. The `context` object give you access to all of the entities from your Wasp app.
You can use this `io` object to register callbacks for all the regular [Socket.IO events](https://socket.io/docs/v4/server-api/). Also, if a user is logged in, you will have a `socket.data.user` on the server.
This is how we can define our `webSocketFn` function:
```ts title="src/webSocket.ts"
import { v4 as uuidv4 } from "uuid"
import { type WebSocketDefinition, type WaspSocketData } from "wasp/server/webSocket"
export const webSocketFn: WebSocketFn = (io, context) => {
io.on("connection", (socket) => {
const username = socket.data.user?.getFirstProviderUserId() ?? "Unknown"
console.log("a user connected: ", username)
socket.on("chatMessage", async (msg) => {
console.log("message: ", msg)
io.emit("chatMessage", { id: uuidv4(), username, text: msg })
// You can also use your entities here:
// await context.entities.SomeEntity.create({ someField: msg })
})
})
}
// Typing our WebSocket function with the events and payloads
// allows us to get type safety on the client as well
type WebSocketFn = WebSocketDefinition<
ClientToServerEvents,
ServerToClientEvents,
InterServerEvents,
SocketData
>
interface ServerToClientEvents {
chatMessage: (msg: { id: string, username: string, text: string }) => void;
}
interface ClientToServerEvents {
chatMessage: (msg: string) => void;
}
interface InterServerEvents {}
// Data that is attached to the socket.
// NOTE: Wasp automatically injects the JWT into the connection,
// and if present/valid, the server adds a user to the socket.
interface SocketData extends WaspSocketData {}
```
### Using the WebSocket On The Client
:::info[Full-stack type safety]
All the hooks we use are typed with the events and payloads you defined on the server. VS Code will give you autocomplete for the events and payloads, and you will get type errors if you make a mistake.
:::
#### The `useSocket` Hook
Client access to WebSockets is provided by the `useSocket` hook. It returns:
- `socket: Socket` for sending and receiving events.
- `isConnected: boolean` for showing a display of the Socket.IO connection status.
- Note: Wasp automatically connects and establishes a WebSocket connection from the client to the server by default, so you do not need to explicitly `socket.connect()` or `socket.disconnect()`.
- If you set `autoConnect: false` in your Wasp file, then you should call these as needed.
All components using `useSocket` share the same underlying `socket`.
#### The `useSocketListener` Hook
Additionally, there is a `useSocketListener: (event, callback) => void` hook which is used for registering event handlers. It takes care of unregistering the handler on unmount.
Wasp's **full-stack type safety** kicks in here: all the event types and payloads are automatically inferred from the server and are available on the client.
You can additionally use the `ClientToServerPayload` and `ServerToClientPayload` helper types to get the payload type for a specific event.
```tsx title="src/ChatPage.tsx"
import React, { useState } from "react"
import {
useSocket,
useSocketListener,
ServerToClientPayload,
} from "wasp/client/webSocket"
export const ChatPage = () => {
const [messageText, setMessageText] = useState<
// We are using a helper type to get the payload type for the "chatMessage" event.
ClientToServerPayload<"chatMessage">
>("")
const [messages, setMessages] = useState<
ServerToClientPayload<"chatMessage">[]
>([])
// The "socket" instance is typed with the types you defined on the server.
const { socket, isConnected } = useSocket()
// This is a type-safe event handler: "chatMessage" event and its payload type
// are defined on the server.
useSocketListener("chatMessage", logMessage)
function logMessage(msg: ServerToClientPayload<"chatMessage">) {
setMessages((priorMessages) => [msg, ...priorMessages])
}
function handleSubmit(e: React.FormEvent) {
e.preventDefault()
// This is a type-safe event emitter: "chatMessage" event and its payload type
// are defined on the server.
socket.emit("chatMessage", messageText)
setMessageText("")
}
const messageList = messages.map((msg) => (
>
)
}
```
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/WebSocket)
#### [WebSocket ยป](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/WebSocket)
[All the options for the webSocket field of the app spec.](https://wasp.sh/docs/api/@wasp.sh/spec/interfaces/WebSocket)
## Advanced Features / Accessing the configuration
Whenever you start a Wasp app, you are starting two processes.
- **The client process** - A React app that implements your app's frontend.
During development, this is a dev server with hot reloading. In production, it's a simple process that serves pre-built static files with environment variables embedded during the build (details depend on [how you deploy it](https://wasp.sh/docs/deployment/intro)).
- **The server process** - An Express server that implements your app's backend.
During development, this is an Express server controlled by a [`nodemon`](https://www.npmjs.com/package/nodemon) process that takes care of hot reloading and restarts. In production, it's a regular Express server run using Node.
Check [the introduction](https://wasp.sh/docs) for a more in-depth explanation of Wasp's runtime architecture.
You can configure both processes through environment variables. See [the deployment instructions](https://wasp.sh/docs/project/env-vars) for a full list of supported variables.
Wasp gives you runtime access to the processes' configurations through **configuration objects**.
### Server configuration object
The server configuration object contains these fields:
- `frontendUrl: String` - Set it with env var `WASP_WEB_CLIENT_URL`.
The URL of your client (the app's frontend).\
Wasp automatically sets it during development when you run `wasp start`.\
In production, you should set it to your client's URL as the server sees it (i.e., with the DNS and proxies considered).
You can access it like this:
```js
import { config } from 'wasp/server'
console.log(config.frontendUrl)
```
### Client configuration object
The client configuration object contains these fields:
- `apiUrl: String` - Set it with env var `REACT_APP_API_URL`
The URL of your server (the app's backend).\
Wasp automatically sets it during development when you run `wasp start`.\
In production, it should contain the value of your server's URL as the user's browser sees it (i.e., with the DNS and proxies considered).
You can access it like this:
```js
import { config } from 'wasp/client'
console.log(config.apiUrl)
```
## Advanced Features / Custom HTTP API Endpoints
In Wasp, the default client-server interaction mechanism is through [Operations](https://wasp.sh/docs/data-model/operations/overview). However, if you need a specific URL method/path, or a specific response, Operations may not be suitable for you. For these cases, you can use an `api`. Best of all, they should look and feel very familiar.
### How to Create an API
APIs are used to tie a JS function to a certain endpoint e.g. `POST /something/special`. They are distinct from Operations and have no client-side helpers (like `useQuery`).
To create a Wasp API, you must:
1. Declare the API in Wasp using the `api` constructor
2. Define the API's NodeJS implementation
After completing these two steps, you'll be able to call the API from the client code (via our `ky` wrapper), or from the outside world.
#### Specifying the API in Wasp
First, we need to declare the API in the Wasp file and you can easily do this with the `api` function:
```ts title="main.wasp.ts"
import { api, app } from "@wasp.sh/spec"
import { fooBar } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
api("GET", "/foo/bar", fooBar),
],
})
```
:::note
When `main.wasp.ts` needs to point to your code, it uses imports like this:
```ts
import { MainPage } from "./src/MainPage" with { type: "ref" }
```
Notice the `with { type: "ref" }` part at the end of the import statement. This tells Wasp to treat the import as a reference to your app's code, without running the imported code. For more details and examples, see [reference imports](https://wasp.sh/docs/general/spec#reference-imports).
:::
Read more about the supported fields in the [API Reference](#api-reference).
#### Defining the API's NodeJS Implementation
:::note
To make sure the Wasp compiler generates the types for APIs for use in the NodeJS implementation, you should add your `api`s to your Wasp file first *and* keep the `wasp start` command running.
:::
After you defined the API, it should be implemented as a NodeJS function that takes three arguments:
1. `req`: Express Request object
2. `res`: Express Response object
3. `context`: An additional context object **injected into the API by Wasp**. This object contains user session information, as well as information about entities. The examples here won't use the context for simplicity purposes. You can read more about it in the [section about using entities in APIs](#using-entities-in-apis).
```ts title="src/apis.ts"
import type { FooBar } from "wasp/server/api";
export const fooBar: FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};
```
:::note
The `FooBar` type is generated by Wasp based on the `api` spec above.
:::
##### Providing Extra Type Information
We'll see how we can provide extra type information to an API function.
Let's say you wanted to create some `GET` route that would take an email address as a param, and provide them the answer to "Life, the Universe and Everything." ๐ What would this look like in TypeScript?
Define the API in Wasp:
```ts title="main.wasp.ts"
import { api, app } from "@wasp.sh/spec"
import { fooBar } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
api("GET", "/foo/bar/:email", fooBar, { entities: ["Task"] }),
],
})
```
We can use the `FooBar` type to which we'll provide the generic **params** and **response** types, which then gives us full type safety in the implementation.
```ts title="src/apis.ts"
import { FooBar } from "wasp/server/api";
export const fooBar: FooBar<
{ email: string }, // params
{ answer: number } // response
> = (req, res, _context) => {
console.log(req.params.email);
res.json({ answer: 42 });
};
```
### Using the API
#### Using the API externally
To use the API externally, you simply call the endpoint using the method and path you used.
For example, if your app is running at `https://example.com` then from the above you could issue a `GET` to `https://example/com/foo/callback` (in your browser, Postman, `curl`, another web service, etc.).
#### Using the API from the Client
To use the API from your client, including with auth support, you can import the `api` instance from `wasp/client/api`. It is a [ky](https://github.com/sindresorhus/ky) instance pre-configured with the API base URL, authentication, and error handling. For example:
```tsx title="src/pages/SomePage.tsx"
import React, { useEffect } from "react";
import { api } from "wasp/client/api";
async function fetchCustomRoute() {
const data = await api.get("/foo/bar").json();
console.log(data);
}
export const Foo = () => {
useEffect(() => {
fetchCustomRoute();
}, []);
return <>{/* ... */}>;
};
```
##### Making Sure CORS Works
APIs are designed to be as flexible as possible, hence they don't utilize the default middleware like Operations do. As a result, to use these APIs on the client side, you must ensure that CORS (Cross-Origin Resource Sharing) is enabled.
You can do this by defining custom middleware for your APIs in the Wasp file.
For example, an `apiNamespace` is a simple spec used to apply some `middlewareConfigFn` to all APIs under some specific path:
```ts title="main.wasp.ts"
import { apiNamespace, app } from "@wasp.sh/spec"
import { apiMiddleware } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
apiNamespace("/foo", { middlewareConfigFn: apiMiddleware }),
],
})
```
And then in the implementation file (returning the default config):
```ts title="src/apis.ts"
import type { MiddlewareConfigFn } from "wasp/server";
export const apiMiddleware: MiddlewareConfigFn = (config) => {
return config;
};
```
We are returning the default middleware which enables CORS for all APIs under the `/foo` path.
For more information about middleware configuration, please see: [Middleware Configuration](https://wasp.sh/docs/advanced/middleware-config)
### Using Entities in APIs
In many cases, resources used in APIs will be [Entities](https://wasp.sh/docs/data-model/entities). To use an Entity in your API, add it to the `api` spec in Wasp:
```ts title="main.wasp.ts"
import { api, app } from "@wasp.sh/spec"
import { fooBar } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
api("GET", "/foo/bar", fooBar, { entities: ["Task"] }),
],
})
```
Wasp will inject the specified Entity into the APIs `context` argument, giving you access to the Entity's Prisma API:
```ts title="src/apis.ts"
import type { FooBar } from "wasp/server/api";
export const fooBar: FooBar = async (req, res, context) => {
res.json({ count: await context.entities.Task.count() });
};
```
The object `context.entities.Task` exposes `prisma.task` from [Prisma's CRUD API](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud).
### Streaming Responses
You can use streaming responses to send data to the client in chunks as it becomes available. This is useful for:
- **LLM responses** - Stream AI-generated content as it's produced
- **Long-running processes** - Show progress updates in real-time
- **Large datasets** - Send data incrementally to avoid timeouts
#### Creating a Streaming API
To create a streaming API, write a function that uses Express response methods like `res.write()` and `res.end()`:
```ts title="main.wasp.ts"
import { api, app } from "@wasp.sh/spec"
import { getStreamingText } from "./src/streaming" with { type: "ref" }
export default app({
// ...
spec: [
api("POST", "/api/streaming-example", getStreamingText),
],
})
```
Don't forget to set up the CORS middleware. See the [section explaning CORS](#making-sure-cors-works) for details.
```ts title="src/streaming.ts"
import OpenAI from "openai";
import type { GetStreamingText } from "wasp/server/api";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export const getStreamingText: GetStreamingText<
never,
string,
{ message: string }
> = async (req, res) => {
const { message } = req.body;
// Set appropriate headers for streaming.
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.setHeader("Transfer-Encoding", "chunked");
const stream = await client.responses.create({
model: "gpt-5",
input: `Funny response to "${message}"`,
stream: true,
});
for await (const chunk of stream) {
if (chunk.type === "response.output_text.delta") {
// Write each chunk to the response as it arrives.
res.write(chunk.delta);
}
}
// End the response.
res.end();
};
```
#### Consuming Streaming Responses
You can consume streaming responses on the client using the `api` instance from `wasp/client/api`. Since ky is built on `fetch`, you get native streaming support via the `Response.body` readable stream. The `api` instance handles authentication automatically.
```tsx title="src/StreamingPage.tsx"
import { useEffect, useState } from "react";
import { api } from "wasp/client/api";
export function StreamingPage() {
const { response } = useTextStream("/api/streaming-example", {
message: "Best Office episode?",
});
return (
Streaming Example
{response}
);
}
function useTextStream(path: string, payload: { message: string }) {
const [response, setResponse] = useState("");
useEffect(() => {
const controller = new AbortController();
fetchStream(
path,
payload,
(chunk) => {
setResponse((prev) => prev + chunk);
},
controller.signal,
);
return () => {
controller.abort();
};
}, [path]);
return { response };
}
async function fetchStream(
path: string,
payload: { message: string },
onData: (data: string) => void,
signal: AbortSignal,
) {
try {
const response = await api.post(path, {
json: payload,
signal,
});
if (response.body === null) {
throw new Error("Stream body is null");
}
const stream = response.body.pipeThrough(new TextDecoderStream());
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
onData(value);
}
} catch (error: unknown) {
if (error instanceof Error) {
if (error.name === "AbortError") {
// Fetch was aborted, no need to log an error
return;
}
console.error("Fetch error:", error.message);
} else {
throw error;
}
}
}
```
### API Reference
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/api)
#### [api ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/api)
[All the options for declaring an API endpoint in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/api)
[API reference](https://wasp.sh/docs/api/@wasp.sh/spec/functions/apiNamespace)
#### [apiNamespace ยป](https://wasp.sh/docs/api/@wasp.sh/spec/functions/apiNamespace)
[All the options for declaring an API namespace in the Wasp spec.](https://wasp.sh/docs/api/@wasp.sh/spec/functions/apiNamespace)
## Advanced Features / Configuring Middleware
Wasp comes with a minimal set of useful Express middleware in every application. While this is good for most users, we realize some may wish to add, modify, or remove some of these choices both globally, or on a per-`api`/path basis.
### Default Global Middleware ๐
Wasp's Express server has the following middleware by default:
- [Helmet](https://helmetjs.github.io/): Helmet helps you secure your Express apps by setting various HTTP headers. *It's not a silver bullet, but it's a good start.*
- [CORS](https://github.com/expressjs/cors#readme): CORS is a package for providing a middleware that can be used to enable [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) with various options.
:::note
CORS middleware is required for the frontend to communicate with the backend.
:::
- [Morgan](https://github.com/expressjs/morgan#readme): HTTP request logger middleware.
- [express.json](https://expressjs.com/en/api.html#express.json) (which uses [body-parser](https://github.com/expressjs/body-parser#bodyparserjsonoptions)): parses incoming request bodies in a middleware before your handlers, making the result available under the `req.body` property.
:::note
JSON middleware is required for [Operations](https://wasp.sh/docs/data-model/operations/overview) to function properly.
:::
- [express.urlencoded](https://expressjs.com/en/api.html#express.urlencoded) (which uses [body-parser](https://expressjs.com/en/resources/middleware/body-parser.html#bodyparserurlencodedoptions)): returns middleware that only parses urlencoded bodies and only looks at requests where the `Content-Type` header matches the type option.
- [cookieParser](https://github.com/expressjs/cookie-parser#readme): parses Cookie header and populates `req.cookies` with an object keyed by the cookie names.
### Customization
You have three places where you can customize middleware:
1. [global](#1-customize-global-middleware): here, any changes will apply by default *to all operations (`query` and `action`) and `api`.* This is helpful if you wanted to add support for multiple domains to CORS, for example.
:::caution[Modifying global middleware]
Please treat modifications to global middleware with extreme care as they will affect all operations and APIs. If you are unsure, use one of the other two options.
:::
2. [per-api](#2-customize-api-specific-middleware): you can override middleware for a specific api route (e.g. `POST /webhook/callback`). This is helpful if you want to disable JSON parsing for some callback, for example.
3. [per-path](#3-customize-per-path-middleware): this is helpful if you need to customize middleware for all methods under a given path.
- It's helpful for things like "complex CORS requests" which may need to apply to both `OPTIONS` and `GET`, or to apply some middleware to a *set of `api` routes*.
[Guide](https://wasp.sh/docs/guides/configuration/cors-multiple-domains)
#### [Multiple Domains CORS ยป](https://wasp.sh/docs/guides/configuration/cors-multiple-domains)
[Configure CORS to support multiple domains in your Wasp application](https://wasp.sh/docs/guides/configuration/cors-multiple-domains)
#### Default Middleware Definitions
Below is the actual definitions of default middleware which you can override.
```ts
export type MiddlewareConfig = Map
// Used in the examples below ๐
export type MiddlewareConfigFn = (middlewareConfig: MiddlewareConfig) => MiddlewareConfig
const defaultGlobalMiddleware: MiddlewareConfig = new Map([
["helmet", helmet()],
["cors", cors({ origin: config.allowedCORSOrigins })],
["logger", logger("dev")],
["express.json", express.json()],
["express.urlencoded", express.urlencoded()],
["cookieParser", cookieParser()]
])
```
### 1. Customize Global Middleware
If you would like to modify the middleware for *all* operations and APIs, you can do something like:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { serverMiddlewareFn } from "./src/serverSetup" with { type: "ref" }
export default app({
name: "myApp",
server: {
middlewareConfigFn: serverMiddlewareFn,
},
// ...
})
```
```ts title="src/serverSetup.ts"
import cors from "cors"
import { config, type MiddlewareConfigFn } from "wasp/server"
export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
// Example of adding extra domains to CORS.
middlewareConfig.set("cors", cors({ origin: [...config.allowedCORSOrigins, "https://example1.com", "https://example2.com"] }))
return middlewareConfig
}
```
### 2. Customize `api`-specific Middleware
If you would like to modify the middleware for a single API, you can do something like:
```ts title="main.wasp.ts"
import { api, app } from "@wasp.sh/spec"
import { webhookCallback, webhookCallbackMiddlewareFn } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
api("POST", "/webhook/callback", webhookCallback, {
middlewareConfigFn: webhookCallbackMiddlewareFn,
auth: false,
}),
],
})
```
```ts title="src/apis.ts"
import express from "express"
import { type WebhookCallback } from "wasp/server/api"
import { type MiddlewareConfigFn } from "wasp/server"
export const webhookCallback: WebhookCallback = (req, res, _context) => {
res.json({ msg: req.body.length })
}
export const webhookCallbackMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
console.log("webhookCallbackMiddlewareFn: Swap express.json for express.raw")
middlewareConfig.delete("express.json")
middlewareConfig.set("express.raw", express.raw({ type: "*/*" }))
return middlewareConfig
}
```
:::note
This gets installed on a per-method basis. Behind the scenes, this results in code like:
```js
router.post("/webhook/callback", webhookCallbackMiddleware, ...)
```
:::
### 3. Customize Per-Path Middleware
If you would like to modify the middleware for all API routes under some common path, you can define a `middlewareConfigFn` on an `apiNamespace`:
```ts title="main.wasp.ts"
import { apiNamespace, app } from "@wasp.sh/spec"
import { fooBarNamespaceMiddlewareFn } from "./src/apis" with { type: "ref" }
export default app({
// ...
spec: [
apiNamespace("/foo/bar", {
middlewareConfigFn: fooBarNamespaceMiddlewareFn,
}),
],
})
```
```ts title="src/apis.ts"
import express from "express"
import { type MiddlewareConfigFn } from "wasp/server"
export const fooBarNamespaceMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => {
const customMiddleware: express.RequestHandler = (_req, _res, next) => {
console.log("fooBarNamespaceMiddlewareFn: custom middleware")
next()
}
middlewareConfig.set("custom.middleware", customMiddleware)
return middlewareConfig
}
```
:::note
This gets installed at the router level for the path. Behind the scenes, this results in something like:
```js
router.use("/foo/bar", fooBarNamespaceMiddleware)
```
:::
## Advanced Features / Type-Safe Links
If you are using Typescript, Wasp gives you typesafe building blocks for navigation. You get autocompletion on route paths, compile errors when params are missing, and a single source of truth between your `main.wasp.ts` file and your client code.
### Typesafe navigation with components
For navigating between pages inside JSX, Wasp exposes two components from `wasp/client/router`: `Link` for simple links, and `NavLink` when you need to react to navigation state.
#### Simple links with `Link`
Reach for `Link` when you just need to send the user to another page. Given this route:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { TaskPage } from "./src/TaskPage" with { type: "ref" }
export default app({
// ...
spec: [
route("TaskRoute", "/task/:id", page(TaskPage)),
],
})
```
You'd use it like this:
```jsx title="TaskList.tsx"
import { Link } from "wasp/client/router"
export const TaskList = () => {
// ...
return (
{tasks.map((task) => (
{/* ๐ Required and typechecked against the path */}
{task.description}
))}
)
}
```
The `to` prop is autocompleted from the routes you defined in `main.wasp.ts`, and `params` is typechecked against the path you picked. Rename a route or change a param, and any broken `Link` is pointed out by Typescript.
#### Reacting to navigation state with `NavLink`
Use `NavLink` when the current page should be highlighted, or when you want to show a spinner during a pending transition. It takes the same props as `Link`, but `className`, `style`, and `children` can be render-prop functions that receive `{ isActive, isPending, isTransitioning }`.
```tsx title="Navigation.tsx"
import { NavLink } from "wasp/client/router"
export const Navigation = () => {
return (
)
}
```
Everything below applies to both `Link` and `NavLink`.
#### Catch-all routes
If a route path ends with a `/*` pattern (also known as [splat](https://reactrouter.com/8.0.1/start/declarative/routing#splats)), pass the rest of the path as the `*` param:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { CatchAllPage } from "./src/CatchAllPage" with { type: "ref" }
export default app({
// ...
spec: [
route("CatchAllRoute", "/pages/*", page(CatchAllPage)),
],
})
```
```jsx title="TaskList.tsx"
About
```
This renders as `/pages/about`.
#### Optional static segments
If a route has an optional static segment, you can choose at the call site whether to include it or not:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { OptionalPage } from "./src/OptionalPage" with { type: "ref" }
export default app({
// ...
spec: [
route("OptionalRoute", "/task/:id/details?", page(OptionalPage)),
],
})
```
```jsx title="TaskList.tsx"
/* You can include the optional segment ... */
Task 1
/* ... or leave it out */
Task 1
```
#### Search params and hash
You can also pass `search` and `hash` to attach a query string and fragment:
```tsx title="TaskList.tsx"
{task.description}
```
This renders as `/task/1?sortBy=date#comments`. Check out the [API Reference](#link-component) for the full list of accepted props.
### Typesafe navigation outside of components
When you need a URL string instead of a component, for example for `useNavigate`, redirects, `window.location`, or anywhere you are not rendering JSX, use the `routes` object from `wasp/client/router`:
```jsx title="TaskList.tsx"
import { routes } from "wasp/client/router"
const linkToTask = routes.TaskRoute.build({ params: { id: 1 } })
```
`linkToTask` is the string `/task/1`. Each route from `main.wasp.ts` shows up on `routes` with a `build` function whose options are typed against the route's path, so the same compile-time safety you get from `Link` is also available outside of JSX.
`build` follows the same rules as the [components above](#typesafe-navigation-with-components): catch-all routes take a `*` param, optional static segments pick a concrete `path`, and you can attach a query string and fragment via `search` and `hash`.
```tsx
const linkToTaskComments = routes.OptionalRoute.build({
path: "/task/:id/details",
params: { id: 1 },
search: { sortBy: "date" },
hash: "comments",
})
```
This renders as `/task/1/details?sortBy=date#comments`. Check out the [API Reference](#routes-object) for the full shape.
### API Reference
#### `Link` Component
The `Link` component accepts the following props:
- `to` required
- A valid Wasp Route path from your `main.wasp.ts` file.
In the case of optional static segments, you must provide one of the possible paths which include or exclude the optional segment. For example, if the path is `/task/:id/details?`, you must provide either `/task/:id/details` or `/task/:id`.
- `params: { [name: string]: string | number }` required (if the path contains params)
- An object with keys and values for each param in the path.
- For example, if the path is `/task/:id`, then the `params` prop must be `{ id: 1 }`. Wasp supports required and optional params.
- `search: string[][] | Record | string | URLSearchParams`
- Any valid input for `URLSearchParams` constructor.
- For example, the object `{ sortBy: 'date' }` becomes `?sortBy=date`.
- `hash: string`
- all other props that the `react-router`'s [Link](https://reactrouter.com/8.0.1/api/components/Link) component accepts
#### `NavLink` Component
The `NavLink` component accepts the following props:
- `to` required
- A valid Wasp Route path from your `main.wasp.ts` file.
In the case of optional static segments, you must provide one of the possible paths which include or exclude the optional segment. For example, if the path is `/task/:id/details?`, you must provide either `/task/:id/details` or `/task/:id`.
- `params: { [name: string]: string | number }` required (if the path contains params)
- An object with keys and values for each param in the path.
- For example, if the path is `/task/:id`, then the `params` prop must be `{ id: 1 }`. Wasp supports required and optional params.
- `search: string[][] | Record | string | URLSearchParams`
- Any valid input for `URLSearchParams` constructor.
- For example, the object `{ sortBy: 'date' }` becomes `?sortBy=date`.
- `hash: string`
- all other props that the `react-router`'s [NavLink](https://reactrouter.com/8.0.1/api/components/NavLink) component accepts
- Notably, `className`, `style`, and `children` accept render-prop functions that receive `{ isActive, isPending, isTransitioning }`, and `end` and `caseSensitive` control how the active match is computed.
#### `routes` Object
The `routes` object contains a function for each route in your app.
```ts title="router.tsx"
export const routes = {
// RootRoute has a path like "/"
RootRoute: {
build: (options?: {
search?: string[][] | Record | string | URLSearchParams
hash?: string
}) => // ...
},
// DetailRoute has a path like "/task/:id/:userId?"
DetailRoute: {
build: (
options: {
params: { id: ParamValue; userId?: ParamValue; },
search?: string[][] | Record | string | URLSearchParams
hash?: string
}
) => // ...
},
// OptionalRoute has a path like "/task/:id/details?"
OptionalRoute: {
build: (
options: {
path: "/task/:id/details" | "/task/:id",
params: { id: ParamValue },
search?: string[][] | Record | string | URLSearchParams
hash?: string
}
) => // ...
},
// CatchAllRoute has a path like "/pages/*"
CatchAllRoute: {
build: (
options: {
params: { "*": ParamValue },
search?: string[][] | Record | string | URLSearchParams
hash?: string
}
) => // ...
},
}
```
The `params` object is required if the route contains params. The `search` and `hash` parameters are optional.
You can use the `routes` object like this:
```tsx
import { routes } from "wasp/client/router"
const linkToRoot = routes.RootRoute.build()
const linkToTask = routes.DetailRoute.build({ params: { id: 1 } })
const linkToOptional = routes.DetailRoute.build({
path: "/task/:id/details",
params: { id: 1 },
})
const linkToCatchAll = routes.CatchAllRoute.build({
params: { "*": "about" },
})
```
## Advanced Features / Routing
Wasp uses [React Router](https://reactrouter.com) under the hood. Route paths support all the standard patterns described below.
### Dynamic route segments {#dynamic-segments}
#### Parameter Segments
Use `:paramName` in a route path to match any value in that segment. Access the matched value in your page component with the `useParams` hook from `react-router`.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { PhotoPage } from "./src/PhotoPage" with { type: "ref" }
export default app({
// ...
spec: [
route("PhotoRoute", "/photo/:photoId", page(PhotoPage)),
],
})
```
```tsx title="src/PhotoPage.tsx"
import { useParams } from "react-router";
export function PhotoPage() {
const { photoId } = useParams<"photoId">();
return
Viewing photo {photoId}
;
}
```
Read more in the [React Router docs on dynamic segments](https://reactrouter.com/8.0.1/start/data/routing#dynamic-segments).
#### Optional Segments
Append `?` to a path segment to make it optional. The route will match whether or not the segment is present.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { PhotoPage } from "./src/PhotoPage" with { type: "ref" }
export default app({
// ...
spec: [
route("PhotoRoute", "/photo/:photoId/edit?", page(PhotoPage)),
],
})
```
```tsx title="src/PhotoPage.tsx"
import { useParams, useLocation } from "react-router";
export function PhotoPage() {
const { photoId } = useParams<"photoId">();
const { pathname } = useLocation();
const isEditing = pathname.endsWith("/edit");
return (
);
}
```
Read more in the [React Router docs on optional segments](https://reactrouter.com/8.0.1/start/data/routing#optional-segments).
#### Splats
Use `/*` at the end of a route path to match any remaining path segments. Access the matched portion with the `'*'` param.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { FilesPage } from "./src/FilesPage" with { type: "ref" }
export default app({
// ...
spec: [
route("FilesRoute", "/files/*", page(FilesPage)),
],
})
```
```tsx title="src/FilesPage.tsx"
import { useParams } from "react-router";
export function FilesPage() {
const { "*": filePath } = useParams();
// Visiting /files/docs/report.txt โ filePath = "docs/report.txt"
return
File: {filePath}
;
}
```
Read more in the [React Router docs on splats](https://reactrouter.com/8.0.1/start/data/routing#splats).
### Lazy-Loaded Routes
By default, Wasp lazy-loads all page routes using React Router's [`lazy`](https://reactrouter.com/how-to/code-splitting) property. This means each page's code is only downloaded when the user navigates to it, resulting in smaller initial bundle sizes. This is especially useful for apps with many routes.
If you need a specific route to be eagerly loaded (included in the main bundle), you can set `lazy: false` on the route spec:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { DashboardPage } from "./src/DashboardPage" with { type: "ref" }
// This route's page will be included in the initial bundle
export default app({
// ...
spec: [
route("DashboardRoute", "/dashboard", page(DashboardPage), { lazy: false }),
],
})
```
:::note
Most apps won't need to change this. Disabling lazy loading is useful when you want to avoid the brief loading delay for a page that users navigate to very frequently, at the cost of a larger initial download.
:::
### Prerendered routes
You can prerender specific routes at build time by setting the `prerender` property. This generates static HTML that is served immediately, giving faster load times and better SEO.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LandingPage } from "./src/LandingPage" with { type: "ref" }
export default app({
// ...
spec: [
route("LandingRoute", "/", page(LandingPage), { prerender: true }),
],
})
```
See the [Prerendering](https://wasp.sh/docs/advanced/prerendering) page for the full documentation.
## Advanced Features / Prerendering
By default, Wasp apps are single-page applications: the browser downloads JavaScript, and React renders the page on the client. This means search engines, AI crawlers, and users on slow connections see a blank page until JavaScript loads and executes.
Wasp can **prerender** specific routes at build time, producing static HTML files that are served immediately. The page then hydrates on the client for full interactivity.
This gives you:
- **Better SEO:** search engines index real HTML content instead of an empty shell.
- **LLM and AI readability:** AI crawlers (ChatGPT, Perplexity, Claude, etc.) can read your content directly.
- **Faster performance on user experience:** users see content immediately (better [Largest Contentful Paint](https://developer.chrome.com/docs/lighthouse/performance/lighthouse-largest-contentful-paint)), with no layout shift from content loading in (better [Cumulative Layout Shift](https://web.dev/articles/cls)).
- **Works without JavaScript:** content is visible even before the browser loads your JS bundle.
### Enabling prerendering
You can add the `prerender` option to a Route spec to enable prerendering. The only [limitation](#limitations) is that the route must **not** have the `authRequired` property turned on.
Once `prerender` is enabled, Wasp will know to render this route's HTML on `wasp build`. The generated HTML is served directly to browsers and crawlers, then we [hydrate](https://react.dev/reference/react-dom/client/hydrateRoot) the page for full interactivity.
#### Static routes
For routes with no [dynamic segments](https://wasp.sh/docs/advanced/routing#dynamic-segments), you can just set `prerender: true`:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { AboutPage } from "./src/AboutPage" with { type: "ref" }
import { LandingPage } from "./src/LandingPage" with { type: "ref" }
export default app({
// ...
spec: [
route("LandingRoute", "/", page(LandingPage), {
prerender: true,
}),
route("AboutRoute", "/about", page(AboutPage), {
prerender: true,
}),
],
})
```
#### Dynamic routes
If your route has [dynamic segments](https://wasp.sh/docs/advanced/routing#dynamic-segments), you'll need to declare with which data you want to prerender them, by passing an array of concrete paths:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { CountryPage } from "./src/CountryPage" with { type: "ref" }
export default app({
// ...
spec: [
route("CountryRoute", "/supported-countries/:country", page(CountryPage), {
prerender: ["/supported-countries/us", "/supported-countries/es"],
}),
],
})
```
The prerendered paths must match the route's pattern, with all dynamic segments replaced by concrete values. All paths not declared in the `prerender` array will still work, but they will be rendered on the client as normal, instead of being prerendered.
#### Generating prerendered paths from data
If you have a lot of dynamic paths to prerender, you can generate the list of paths from your data or other sources. For example, if you want to prerender the routes for your Top 10 countries, you can fetch them from your analytics and generate the paths:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { CountryPage } from "./src/CountryPage" with { type: "ref" }
async function getTopCountries(): Promise {
const response = await fetch("https://api.my-analytics.com/top-countries")
return await response.json()
}
export default app({
// ...
spec: [
route("CountryRoute", "/supported-countries/:country", page(CountryPage), {
prerender: (await getTopCountries()).map(country => `/supported-countries/${country}`),
}),
],
})
```
[Wasp Spec files](https://wasp.sh/docs/general/spec) run like regular Node.js scripts, so you can use any Node.js APIs (like `fs`, `path`, or `fetch`), or npm libraries, to generate the prerendered paths.
### How it works
By default, `wasp build` generates a single `200.html` file that serves as the entry point for all routes. When a request comes in, the server sends this HTML, and React renders the appropriate page on the client. This is called a Single-Page Application (SPA) architecture.
But for prerendered routes, Wasp will call them at build time, and render your page components as HTML, with special markers to allow for hydration. This HTML is then written to a file placed in the build output alongside the SPA file.
When a request hits a prerendered route's path, the server sends the pre-built HTML directly. Once the browser loads the JavaScript bundle, React hydrates the static HTML into a fully interactive app, no second render needed.
Routes that haven't enabled prerendering continue to work as before: the server sends the SPA file, and the client renders the page from scratch.
### When to use prerendering
Prerendering works best for pages where the content is known at build time:
- Landing pages and marketing pages
- About, pricing, and FAQ pages
- Blog posts or documentation
- Any page with mostly static content that doesn't depend on the logged-in user
:::tip
Prerendering is especially valuable if you want your content to be indexed by search engines or readable by AI assistants like ChatGPT, Perplexity, or Claude.
:::
You can learn more in our SEO guide, which explains exactly how prerendering helps search engines index your content:
[Guide](https://wasp.sh/docs/guides/optimization/seo)
#### [SEO guide ยป](https://wasp.sh/docs/guides/optimization/seo)
[Learn how to make your Wasp app more discoverable by search engines.](https://wasp.sh/docs/guides/optimization/seo)
### Limitations
#### No auth-required pages
Routes pointing to pages with `authRequired: true` cannot be prerendered, since the page content depends on the logged-in user.
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { DashPage } from "./src/DashPage" with { type: "ref" }
export default app({
// ...
spec: [
// โ Won't compile (authRequired is true)
route(
"DashRoute",
"/dashboard",
page(DashPage, { authRequired: true }),
{ prerender: true }
),
],
})
```
Wasp reports an error at compile time if you try to prerender an auth-required page.
### Troubleshooting
#### Hydration mismatches
When React hydrates a prerendered page, it expects the prerendered HTML to match what the client renders. If they differ, React logs a warning and may discard the prerendered HTML, losing the performance benefits.
##### Common causes
- **Checking for `window` or `document`:** code like `typeof window !== 'undefined'` or `import.meta.env.SSR` returns different values on the prerender vs. the client, and might change everything that depends on it.
- **Non-deterministic values during render:** functions like `Date.now()`, or `Math.random()` produces different results on each render.
- **Browser-only APIs:** accessing `window.innerWidth`, `navigator.userAgent`, `localStorage`, or similar APIs during render will fail while prerendering. This also applies to some third-party libraries that access these APIs, or less obvious JS APIs like `Intl.DateTimeFormat`, which can use different timezones and locales on the prerender vs. client.
##### How to fix: the `useIsClient` pattern
The fix is to render the same content on both the prerender and the client during the initial render, then add client-only behavior after hydration using `useEffect`.
Here's an example of the **wrong** approach:
```tsx title="src/LandingPage.tsx"
// โ Causes a hydration mismatch
export function LandingPage() {
const isClient = typeof window !== "undefined";
return
);
};
```
### Using a Different OAuth Provider
[Arctic](https://v1.arcticjs.dev/) (v1) supports many providers. Check its documentation for the full list and their specific setup requirements.
Each provider follows the same pattern. For example, to use Twitch instead of Spotify:
- Swap the Arctic provider: `new arctic.Twitch(clientId, clientSecret, redirectURI)`.
- Fetch the user from `https://api.twitch.tv/helix/users` with the appropriate scopes (e.g., `user:read:email`).
- Update the `User` schema and the fields passed to `createUser` to match the provider's response.
## Integrations / File Uploads
:::note
Last checked with Wasp 0.24 and multer 2.1.1.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to implement file uploads in your Wasp application using [Multer](https://github.com/expressjs/multer).
### Setting up File Uploads
#### 1. Install Multer
Install the Multer package and its types:
```bash
npm install multer
npm install --save-dev @types/multer
```
#### 2. Define the API endpoint in main.wasp.ts
Create an API namespace with middleware configuration and the upload endpoint:
```ts title="main.wasp.ts"
import { api, apiNamespace, app, page, route } from "@wasp.sh/spec"
import { configureFileUploadMiddleware, uploadFile } from "./src/apis" with { type: "ref" }
import { MainPage } from "./src/MainPage" with { type: "ref" }
export default app({
// ...
spec: [
route("RootRoute", "/", page(MainPage)),
apiNamespace("/api/upload", { middlewareConfigFn: configureFileUploadMiddleware }),
api("POST", "/api/upload", uploadFile),
],
})
```
#### 3. Create the API handlers
Create the middleware configuration and upload handler:
```ts title="src/apis.ts"
import type { MiddlewareConfigFn } from "wasp/server";
import type { UploadFile } from "wasp/server/api";
import multer from "multer";
const upload = multer({ dest: "uploads/" });
export const configureFileUploadMiddleware: MiddlewareConfigFn = (config) => {
config.set("multer", upload.single("file"));
return config;
};
export const uploadFile: UploadFile = (req, res) => {
console.log(req.body);
console.log(req.file);
const file = req.file!;
return res.json({
fileExists: !!file,
});
};
```
#### 4. Create the upload form
Create a form component to handle file uploads:
```tsx title="src/MainPage.tsx"
import { useState } from "react";
import { api } from "wasp/client/api";
export const MainPage = () => {
const [name, setName] = useState("");
const [file, setFile] = useState();
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!file) return;
const formData = new FormData();
formData.append("name", name);
formData.append("file", file);
const data = await api
.post("/api/upload", { body: formData })
.json<{ fileExists: boolean }>();
alert(JSON.stringify(data, null, 2));
};
return (
);
};
```
### Customizing Upload Settings
#### Change upload destination
You can customize where files are stored:
```ts
const upload = multer({ dest: "my-custom-uploads/" });
```
#### Limit file size
Add file size limits:
```ts
const upload = multer({
dest: "uploads/",
limits: {
fileSize: 5 * 1024 * 1024, // 5MB limit
},
});
```
#### Filter file types
Only accept certain file types:
```ts
const upload = multer({
dest: "uploads/",
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith("image/")) {
cb(null, true);
} else {
cb(new Error("Only images are allowed"));
}
},
});
```
#### Handle multiple files
To handle multiple file uploads:
```ts
export const configureFileUploadMiddleware: MiddlewareConfigFn = (config) => {
config.set("multer", upload.array("files", 10)); // Max 10 files
return config;
};
```
For more options, see the [Multer documentation](https://github.com/expressjs/multer).
## Integrations / Sentry
:::note
Last checked with Wasp 0.24, @sentry/node 8, and @sentry/react 8.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to integrate [Sentry](https://sentry.io/) into your Wasp application for error tracking on both the server and client.
### Prerequisites
- A Wasp project set up
- A [Sentry account](https://sentry.io/signup/)
### Setting up Sentry
#### 1. Create Sentry Projects
You'll need to create two projects in Sentry:
1. **Server project**: Select `Node.js` as the platform and `Express` as the framework
2. **Client project**: Select `React` as the platform
After creating each project, you'll receive a unique DSN (Data Source Name) that you'll use to configure Sentry.
#### 2. Install Sentry packages
Install the Sentry SDKs:
```bash
npm install @sentry/node @sentry/react
```
#### 3. Configure your Wasp file
Add both the server and client setup functions to your `main.wasp.ts`:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { setupClient } from "./src/clientSetup" with { type: "ref" }
import { setupServer } from "./src/serverSetup" with { type: "ref" }
export default app({
name: "MyApp",
wasp: { version: "^0.24.0" },
title: "my-app",
head: [""],
server: {
setupFn: setupServer,
},
client: {
setupFn: setupClient,
},
// ...
})
```
#### 4. Configure Server-Side Sentry
Create the server setup file:
```ts title="src/serverSetup.ts"
import * as Sentry from "@sentry/node";
import { ServerSetupFn } from "wasp/server";
Sentry.init({
dsn: process.env.SENTRY_SERVER_DSN,
environment: process.env.NODE_ENV,
tracesSampleRate: 1.0,
});
export const setupServer: ServerSetupFn = async ({ app }) => {
Sentry.setupExpressErrorHandler(app);
};
```
:::note
Find your DSN in Sentry under **Settings > Client Keys (DSN)**.
:::
#### 5. Configure Client-Side Sentry
Create the client setup file:
```ts title="src/clientSetup.ts"
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: import.meta.env.REACT_APP_SENTRY_CLIENT_DSN,
environment: import.meta.env.MODE,
tracesSampleRate: 1.0,
});
export const setupClient = async () => {
// Sentry is initialized above, before the setup function runs.
// You can add additional client-side setup here if needed.
};
```
:::note
The `setupFn` must be defined and exported even if it has no additional logic. Sentry's `init` call runs at module load time, which is before Wasp calls the setup function.
:::
#### 6. Set up environment variables
Add to your `.env.server`:
```bash title=".env.server"
SENTRY_SERVER_DSN=https://your-server-dsn@sentry.io/your-project-id
```
Add to your `.env.client`:
```bash title=".env.client"
REACT_APP_SENTRY_CLIENT_DSN=https://your-client-dsn@sentry.io/your-project-id
```
### Testing the Integration
#### Test Server Errors
Create an API endpoint that throws an error:
```ts title="src/apis.ts"
import { TestError } from "wasp/server/api";
export const testError: TestError = async (req, res) => {
throw new Error("Test server error for Sentry");
};
```
#### Test Client Errors
Add a button that triggers an error:
```tsx title="src/MainPage.tsx"
export const MainPage = () => {
const handleError = () => {
throw new Error("Test client error for Sentry");
};
return (
);
};
```
### Advanced Configuration
#### Adding User Context
Track which user encountered an error:
```ts title="src/serverSetup.ts"
import * as Sentry from "@sentry/node";
// In your API handlers or operations
export const someOperation = async (args, context) => {
if (context.user) {
Sentry.setUser({
id: context.user.id,
email: context.user.email,
});
}
// ...
};
```
#### Performance Monitoring
Enable performance monitoring:
```ts
Sentry.init({
dsn: "your-dsn",
tracesSampleRate: 0.1, // Capture 10% of transactions
profilesSampleRate: 0.1, // Capture 10% of profiles (if using profiling)
});
```
#### Error Boundaries (React)
Use Sentry's error boundary for React:
```tsx title="src/App.tsx"
import * as Sentry from "@sentry/react";
export const App = ({ children }) => {
return (
An error occurred}>
{children}
);
};
```
For more configuration options, see the [Sentry documentation](https://docs.sentry.io/).
## Integrations / Swagger UI
:::note
Last checked with Wasp 0.24, swagger-jsdoc 6, and swagger-ui-express 5.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to add Swagger UI documentation to your Wasp APIs, making it easy to explore and test your endpoints.
### Setting up Swagger UI
#### 1. Install dependencies
Install the required packages:
```bash
npm install swagger-jsdoc swagger-ui-express
npm install --save-dev @types/swagger-jsdoc @types/swagger-ui-express
```
#### 2. Configure the API namespace in main.wasp.ts
Add an API namespace for the Swagger UI endpoint:
```ts title="main.wasp.ts"
import { api, apiNamespace, app } from "@wasp.sh/spec"
import { getStatus } from "./src/apis" with { type: "ref" }
import { swaggerMiddleware } from "./src/swagger-ui" with { type: "ref" }
export default app({
name: "MyApp",
wasp: { version: "^0.24.0" },
title: "my-app",
head: [""],
spec: [
apiNamespace("/api-docs", { middlewareConfigFn: swaggerMiddleware }),
api("GET", "/status", getStatus, { auth: false }),
],
})
```
#### 3. Create the spec generator script
Because `swagger-jsdoc` scans source files at runtime and `src/` is not available in the production Docker image, you need to pre-generate the spec as a TypeScript module that gets bundled with the server.
Create `scripts/generate-swagger.js`:
```js title="scripts/generate-swagger.js"
import swaggerJsdoc from "swagger-jsdoc";
import { writeFileSync } from "fs";
const spec = swaggerJsdoc({
definition: {
openapi: "3.0.0",
info: {
title: "My API",
version: "1.0.0",
description: "API documentation for my Wasp application",
contact: {
name: "API Support",
url: "https://example.com",
email: "support@example.com",
},
},
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
description: "Enter your JWT token in the format: Bearer {token}",
},
},
},
security: [{ bearerAuth: [] }],
},
apis: ["./src/**/*.ts", "!./src/swaggerSpec.ts"],
});
writeFileSync(
"./src/swaggerSpec.ts",
`const swaggerSpec = ${JSON.stringify(spec, null, 2)} as const;\nexport default swaggerSpec;\n`,
);
console.log("swaggerSpec.ts generated");
```
Run this script whenever you add or change JSDoc annotations:
```bash
node scripts/generate-swagger.js
```
This generates `src/swaggerSpec.ts`, which gets bundled into the server and works in both dev and production.
:::note
You may want to add `src/swaggerSpec.ts` to your `.gitignore` since it's a generated file.
:::
#### 4. Create the Swagger middleware
Create the Swagger UI middleware that serves the pre-generated spec:
```ts title="src/swagger-ui.ts"
import * as express from "express";
import helmet from "helmet";
import swaggerUi from "swagger-ui-express";
import { env, MiddlewareConfigFn } from "wasp/server";
import baseSwaggerDoc from "./swaggerSpec";
const swaggerDoc = {
...baseSwaggerDoc,
servers: [{ url: env.WASP_SERVER_URL, description: "API server" }],
};
export const swaggerMiddleware: MiddlewareConfigFn = (middlewareConfig) => {
middlewareConfig.delete("helmet");
middlewareConfig.set(
"helmet",
helmet({
contentSecurityPolicy: false,
hsts: false,
}),
);
swaggerUi.serve.forEach((handler, i) => {
middlewareConfig.set(`swaggerServe${i}`, handler);
});
middlewareConfig.set(
"swaggerSetup",
(
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
return swaggerUi.setup(swaggerDoc, {
explorer: true,
customCss: ".swagger-ui .topbar { display: none }",
swaggerOptions: {
persistAuthorization: true,
url: "/api-docs/swagger.json",
},
})(req, res, next);
},
);
return middlewareConfig;
};
```
#### 5. Document your APIs
Add JSDoc comments with Swagger annotations above your API definitions:
```ts title="src/apis.ts"
import { GetStatus } from "wasp/server/api";
/**
* @swagger
* /status:
* get:
* summary: Get API status
* description: Returns the current status of the API
* tags:
* - Status
* security:
* - bearerAuth: []
* responses:
* 200:
* description: Successful response
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* 401:
* description: Unauthorized
* 500:
* description: Server error
*/
export const getStatus: GetStatus = async (req, res) => {
return res.json({ message: "OK" });
};
```
#### 6. Access the documentation
Start your Wasp application and navigate to `http://localhost:3001/api-docs` to see your API documentation.
### Documenting Different Request Types
#### POST Request with Body
```ts
/**
* @swagger
* /users:
* post:
* summary: Create a new user
* tags:
* - Users
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - email
* - password
* properties:
* email:
* type: string
* format: email
* password:
* type: string
* minLength: 8
* responses:
* 201:
* description: User created successfully
* 400:
* description: Invalid input
*/
```
#### Path Parameters
```ts
/**
* @swagger
* /users/{id}:
* get:
* summary: Get user by ID
* tags:
* - Users
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: User ID
* responses:
* 200:
* description: User found
* 404:
* description: User not found
*/
```
#### Query Parameters
```ts
/**
* @swagger
* /users:
* get:
* summary: List users
* tags:
* - Users
* parameters:
* - in: query
* name: page
* schema:
* type: integer
* default: 1
* description: Page number
* - in: query
* name: limit
* schema:
* type: integer
* default: 10
* description: Items per page
* responses:
* 200:
* description: List of users
*/
```
### Customization
#### Custom Styling
You can customize the Swagger UI appearance:
```ts
swaggerUi.setup(swaggerDoc, {
customCss: `
.swagger-ui .topbar { display: none }
.swagger-ui .info { margin: 20px 0 }
`,
customSiteTitle: "My API Documentation",
customfavIcon: "/favicon.ico",
});
```
#### Group APIs with Tags
Use tags to organize your endpoints. Add a `tags` array to the `definition` in `scripts/generate-swagger.js`:
```ts
const spec = swaggerJsdoc({
definition: {
// ... other config
tags: [
{ name: "Users", description: "User management endpoints" },
{ name: "Posts", description: "Blog post endpoints" },
{ name: "Auth", description: "Authentication endpoints" },
],
},
apis: ["./src/**/*.ts", "!./src/swaggerSpec.ts"],
});
```
For more options, see the [swagger-jsdoc documentation](https://github.com/Surnet/swagger-jsdoc) and [swagger-ui-express documentation](https://github.com/scottie1984/swagger-ui-express).
## Integrations / WebSocket Namespaces
:::note
Last checked with Wasp 0.24.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to use Socket.IO namespaces with Wasp's WebSocket support for organizing your real-time communication channels.
### Understanding Namespaces
Wasp's built-in WebSocket support gives you a single default connection with type-safe events via `useSocket` and `useSocketListener` (see the [Web Sockets docs](https://wasp.sh/docs/advanced/web-sockets)). Namespaces are a Socket.IO feature that lets you split real-time logic over separate channels on a single shared connection. This is useful when you want to separate concerns, for example having a `/chat` namespace for chat-related events and a `/notifications` namespace for notification events.
When using namespaces, you bypass Wasp's built-in client hooks (`useSocket`, `useSocketListener`) and manage connections directly with `socket.io-client`. Wasp still handles the server-side setup and provides the `io` server instance.
### Setting up WebSocket with Namespaces
#### 1. Configure WebSocket in main.wasp.ts
Enable WebSocket in your Wasp spec with `autoConnect: false` since you'll manage connections manually:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import Main from "./src/MainPage" with { type: "ref" }
import { webSocketFn } from "./src/websocketSetup" with { type: "ref" }
export default app({
name: "WebsocketTest",
wasp: { version: "^0.24.0" },
title: "websocket-test",
head: [""],
webSocket: {
fn: webSocketFn,
autoConnect: false,
},
spec: [
route("RootRoute", "/", page(Main)),
],
})
```
#### 2. Create the server-side WebSocket handler
Set up the namespace on the server side. The `webSocketFn` receives the Socket.IO `io` server and a `context` object with access to your entities:
```ts title="src/websocketSetup.ts"
import { type WebSocketDefinition } from "wasp/server/webSocket";
export const webSocketFn: WebSocketDefinition = (io, _context) => {
// Create a namespace for messages
const messagesNamespace = io.of("/messages");
messagesNamespace.on("connection", (socket) => {
console.log("Client connected to messages namespace");
socket.on("chatMessage", (msg) => {
console.log("message: ", msg);
// Broadcast to all clients in the namespace
messagesNamespace.emit("chatMessage", {
id: crypto.randomUUID(),
username: "User",
text: msg,
});
});
socket.on("disconnect", () => {
console.log("Client disconnected from messages namespace");
});
});
};
```
#### 3. Create client-side WebSocket utilities
Create a module to manage the namespace connection on the client. Since you're connecting to a custom namespace, you use `socket.io-client` directly instead of Wasp's built-in `useSocket` hook:
```ts title="src/websocketHooks.ts"
import { useEffect, useState } from "react";
import { io, type Socket } from "socket.io-client";
import { config } from "wasp/client";
const messagesSocket: Socket = io(`${config.apiUrl}/messages`, {
transports: ["websocket"],
// Vite pre-bundles socket.io-client which breaks autoConnect: https://github.com/vitejs/vite/issues/4798
autoConnect: false,
});
messagesSocket.connect();
export function useMessagesSocket(): { socket: Socket; isConnected: boolean } {
const [isConnected, setIsConnected] = useState(messagesSocket.connected);
useEffect(() => {
function onConnect() {
setIsConnected(true);
}
function onDisconnect() {
setIsConnected(false);
}
messagesSocket.on("connect", onConnect);
messagesSocket.on("disconnect", onDisconnect);
return () => {
messagesSocket.off("connect", onConnect);
messagesSocket.off("disconnect", onDisconnect);
};
}, []);
return {
socket: messagesSocket,
isConnected,
};
}
export function useSocketListener(
socket: Socket,
event: string,
handler: (...args: any[]) => void,
) {
useEffect(() => {
socket.on(event, handler);
return () => {
socket.off(event, handler);
};
}, [event, handler, socket]);
}
```
#### 4. Use the WebSocket in your component
Now use the hooks in your React component:
```tsx title="src/MainPage.tsx"
import { useMessagesSocket, useSocketListener } from "./websocketHooks";
const MainPage = () => {
const { socket, isConnected } = useMessagesSocket();
useSocketListener(socket, "chatMessage", (message) => {
console.log("message received: ", message);
});
return (
);
};
export default MainPage;
```
### Multiple Namespaces
You can create multiple namespaces for different purposes:
```ts title="src/websocketSetup.ts"
export const webSocketFn: WebSocketDefinition = (io, _context) => {
// Messages namespace
const messagesNamespace = io.of("/messages");
messagesNamespace.on("connection", (socket) => {
// Handle messages events
});
// Notifications namespace
const notificationsNamespace = io.of("/notifications");
notificationsNamespace.on("connection", (socket) => {
// Handle notification events
});
// Presence namespace
const presenceNamespace = io.of("/presence");
presenceNamespace.on("connection", (socket) => {
// Handle presence events
});
};
```
### Room Support within Namespaces
Namespaces can also use rooms for further organization:
```ts
messagesNamespace.on("connection", (socket) => {
// Join a specific room
socket.on("joinRoom", (roomId) => {
socket.join(roomId);
});
// Send message to a specific room
socket.on("roomMessage", ({ roomId, message }) => {
messagesNamespace.to(roomId).emit("chatMessage", message);
});
});
```
For more information about Socket.IO namespaces, see the [Socket.IO documentation](https://socket.io/docs/v4/namespaces/).
## Legacy / Wasp Installer
:::note
Last checked with Wasp 0.21.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
Starting from Wasp 0.21, installation is done [through npm](https://wasp.sh/docs/quick-start#detailed-installation). The installation method using the script installer is now considered legacy and is not supported anymore. We'll keep it around for the foreseeable future to give users time to switch, but you will not be able to get newer versions until you migrate to npm-based installation.
### How to migrate off the legacy installer {#migrate}
To switch to the new installation method, you can run our migration tool:
```shell
curl -sSL https://get.wasp.sh/installer.sh | sh -s -- migrate-to-npm
```
Afterwards, you can use [the regular installation instructions](https://wasp.sh/docs/quick-start#detailed-installation) to install Wasp through npm:
```shell
npm i -g @wasp.sh/wasp-cli@latest
```
You can also ask for a specific version of Wasp. Wasp versions 0.20.2 and greater are available through npm:
```shell
# Set x.y.z to the version you want to install, e.g. 0.20.2
npm i -g @wasp.sh/wasp-cli@x.y.z
```
### Keep using the legacy installer {#keep-using}
If you haven't yet migrated to the npm installer method, you can keep using the legacy installer, by running:
```shell
# Set x.y.z to the version you want to install, e.g. 0.20.1
# The installer will refuse to run without a specific version argument
curl -sSL https://get.wasp.sh/installer.sh | sh -s -- -v x.y.z
```
You should only use the legacy installer as a stopgap while migrating workstations and CI to npm installations, as it can cause conflicts with the new method. For that reason, the installer will not work in the following cases:
- You have already installed Wasp through npm.
- You have already run the migration tool.
- You are trying to install Wasp >= 0.21.
- You are calling the installer without a version argument.
In any of these cases, the installer will print an error message and exit without installing Wasp. You can still [switch back manually](#switch-back) if needed.
### Troubleshooting
#### I need to use versions older than Wasp 0.20.2
These versions are very out of date and we don't recommend using them. We urge you to upgrade your Wasp project to a newer version as soon as possible to keep your app secure and stable.
In the transition period until these apps are migrated, you will need to [keep using the legacy installer](#keep-using) to install these older versions of Wasp.
#### I need to switch back to the legacy installer {#switch-back}
:::note
If you found a bug in the npm-based Wasp, or a workflow that is no longer possible, please report it to us so we can fix it as soon as possible. You can do that [through a GitHub issue](https://github.com/wasp-lang/wasp/issues/new/choose), or [on our Discord server](https://discord.gg/rzdnErX).
:::
If you have already switched to npm installation but need to switch back to the legacy installer:
1. Uninstall the npm version of Wasp:
```shell
npm uninstall -g @wasp.sh/wasp-cli
```
2. Make sure that Wasp is uninstalled from your system:
```shell
type wasp
```
If Wasp was correctly uninstalled, the `type` command will output "not found". If Wasp has not been completely uninstalled, it will print the path of the `wasp` binary, and you can manually remove it.
3. Remove the npm marker file from your system:
```shell
rm $HOME/.local/share/wasp-lang/.uses-npm
```
4. Run the installer again with the version you need:
```shell
# Set x.y.z to the version you want to install, e.g. 0.20.1
# The installer will refuse to run without a specific version argument
curl -sSL https://get.wasp.sh/installer.sh | sh -s -- -v x.y.z
```
#### "Bad CPU type in executable" on Mac with Mx chip (Apple Silicon)
You have two options to run Wasp on your Mac with Mx chip:
1. **Recommended:** [Migrate to the npm-based installation method](#migrate), which works natively on Apple Silicon.
2. Keep using the legacy installer, but install [Rosetta on your Mac](https://support.apple.com/en-us/HT211861) to enable running x86 binaries.
To install Rosetta, run the following command in your terminal
```bash
softwareupdate --install-rosetta
```
Once installed, Wasp will run on your system as normal.
## Legacy / Wasp DSL
:::note
Last checked with Wasp 0.24.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
Wasp used to have its own configuration language, the **Wasp DSL**, which you wrote in a `main.wasp` file. Starting with Wasp 0.24, the Wasp DSL is now retired in favor of the [Wasp Spec](https://wasp.sh/docs/general/spec): a `main.wasp.ts` file written in TypeScript.
:::tip[Upgrading from Wasp 0.23 to 0.24?]
The conversion below is mechanical, so you can let an LLM do the heavy lifting instead. The [migration guide](https://wasp.sh/docs/0.24/migration-guide#use-an-agent-to-do-it-for-you) has a copyable prompt bundling this guide, the Wasp Spec docs, and the shared migration steps. Once your config is converted, return to the [migration guide](https://wasp.sh/docs/0.24/migration-guide) for the remaining shared steps.
:::
### New features
#### Just TypeScript
The Wasp DSL was a custom language, so it needed its own IDE extension for highlighting and autocompletion, and you couldn't use the JavaScript ecosystem inside it. The Wasp Spec is just TypeScript, so:
- No special IDE extension is needed. You get type checking, autocompletion, and go-to-definition from your editor's regular TypeScript support.
- You can `import` and use npm packages, environment variables, and your own helpers while building the config.
- You can use normal language features (variables, functions, loops, conditionals) to remove repetition from your config.
#### Multiple files
The Wasp DSL kept your entire configuration in a single `main.wasp`. The Wasp Spec lets you split it across multiple `*.wasp.ts` files and import specifications between them, so you can keep large apps organized (for example, a separate `auth.wasp.ts` or `payments.wasp.ts` next to the feature it configures).
See the [Wasp Spec documentation](https://wasp.sh/docs/general/spec#splitting-your-spec-into-multiple-files) for details.
### Changes
#### Overview
| What | Before | After |
| ------------------- | ------------------ | ---------------------------------- |
| File name | `main.wasp` | `main.wasp.ts` |
| Creating an app | `app Name { ... }` | `app({ name, ..., spec: [...] });` |
| Configuring the app | \`\`\` | |
| app Name { | | |
| auth: { ... }, | | |
| server: { ... }, | | |
| } | | |
| \`\`\` | \`\`\` | |
| app({ | | |
| auth: ..., | | |
| server: ..., | | |
| }); | | |
````|
| Adding app specifications | ```
route X { ... }
query X { ... }
action X { ... }
``` | ```
app({
spec: [
route(...),
query(...),
action(...),
]
});
``` |
| Referencing code | `import { x } from "@src/..."` inside a declaration | `import { ... } from "./src/..." with { type: "ref" };` at the top level |
| Entity references | `Task` (identifier) | `"Task"` (string) |
### App, routes, and pages
In the DSL, a `route` points to a `page` by name. In the Wasp Spec, `route` takes the `page` object directly.
**Wasp DSL**
```wasp title="main.wasp"
app todoApp {
title: "ToDo App",
wasp: { version: "^0.24.0" }
}
route MainRoute { path: "/", to: MainPage }
page MainPage {
component: import { MainPage } from "@src/MainPage",
authRequired: true
}
````
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec";
import { MainPage } from "./src/MainPage" with { type: "ref" };
export default app({
name: "todoApp",
title: "ToDo App",
wasp: { version: "^0.24.0" },
spec: [
route("MainRoute", "/", page(MainPage, { authRequired: true })),
],
});
```
Note that `route` no longer references a page by name (`to: MainPage`); it takes the `page(...)` object directly.
#### Queries and actions
**Wasp DSL**
```wasp title="main.wasp"
query getTasks {
fn: import { getTasks } from "@src/queries",
entities: [Task]
}
action createTask {
fn: import { createTask } from "@src/actions",
entities: [Task]
}
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { action, app, query } from "@wasp.sh/spec";
import { getTasks } from "./src/queries" with { type: "ref" };
import { createTask } from "./src/actions" with { type: "ref" };
export default app({
// ...
spec: [
query(getTasks, { entities: ["Task"] }),
action(createTask, { entities: ["Task"] }),
],
});
```
#### APIs: `httpRoute` becomes positional arguments
The DSL's `httpRoute: (GET, "/path")` becomes the first two arguments of `api`.
**Wasp DSL**
```wasp title="main.wasp"
apiNamespace bar {
middlewareConfigFn: import { barNamespaceMiddlewareFn } from "@src/apis",
path: "/bar"
}
api barBaz {
fn: import { barBaz } from "@src/apis",
auth: false,
entities: [Task],
httpRoute: (GET, "/bar/baz")
}
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { api, apiNamespace, app } from "@wasp.sh/spec";
import { barBaz, barNamespaceMiddlewareFn } from "./src/apis" with { type: "ref" };
export default app({
// ...
spec: [
apiNamespace("/bar", {
middlewareConfigFn: barNamespaceMiddlewareFn,
}),
api("GET", "/bar/baz", barBaz, { auth: false, entities: ["Task"] }),
],
});
```
#### Jobs: `perform` is flattened
The DSL's `perform: { fn, executorOptions }` is flattened: `fn` becomes the first argument and `executorOptions` becomes `performExecutorOptions`.
**Wasp DSL**
```wasp title="main.wasp"
job mySpecialJob {
executor: PgBoss,
perform: {
fn: import { foo } from "@src/jobs/bar",
executorOptions: { pgBoss: {=json { "retryLimit": 1 } json=} }
},
entities: [Task]
}
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, job } from "@wasp.sh/spec";
import { foo } from "./src/jobs/bar" with { type: "ref" };
export default app({
// ...
spec: [
job(foo, {
executor: "PgBoss",
entities: ["Task"],
performExecutorOptions: { pgBoss: { retryLimit: 1 } },
}),
],
});
```
#### CRUD
**Wasp DSL**
```wasp title="main.wasp"
crud tasks {
entity: Task,
operations: {
getAll: {},
create: { overrideFn: import { createTask } from "@src/actions" }
}
}
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, crud } from "@wasp.sh/spec";
import { createTask } from "./src/actions" with { type: "ref" };
export default app({
// ...
spec: [
crud("tasks", "Task", {
getAll: {},
create: { overrideFn: createTask },
}),
],
});
```
#### Top-level config: `auth`, `server`, `client`, `db`, `emailSender`, `webSocket`
These were top-level fields of the `app` declaration's dictionary in the DSL. In the Wasp Spec they are keys of the `app({ ... })` object.
**Wasp DSL**
```wasp title="main.wasp"
app todoApp {
title: "ToDo App",
wasp: { version: "^0.24.0" },
auth: {
userEntity: User,
methods: { google: {} },
onAuthFailedRedirectTo: "/login"
},
client: {
rootComponent: import App from "@src/App"
},
emailSender: {
provider: SMTP,
defaultFrom: { email: "hi@example.com" }
}
}
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec";
import App from "./src/App" with { type: "ref" };
export default app({
name: "todoApp",
title: "ToDo App",
wasp: { version: "^0.24.0" },
auth: {
userEntity: "User",
methods: { google: {} },
onAuthFailedRedirectTo: "/login",
},
client: {
rootComponent: App,
},
emailSender: {
provider: "SMTP",
defaultFrom: { email: "hi@example.com" },
},
// ...
});
```
### How to migrate
These steps convert a Wasp DSL config to the Wasp Spec. Before running `wasp install` below, make sure your app's Wasp version is `^0.24.0`.
After finishing this guide, return to the [migration guide](https://wasp.sh/docs/0.24/migration-guide) if you still need to complete the shared Wasp 0.24 migration steps.
Wasp validates the Wasp Spec support files during migration, including the required `package.json` entries, `tsconfig.wasp.json` options, and `tsconfig.src.json` exclusions.
1. Rename `tsconfig.json` to `tsconfig.src.json` and make it exclude Wasp Spec files:
```json title="tsconfig.src.json"
{
// ...
"include": ["src"],
"exclude": ["**/*.wasp.ts"]
}
```
2. Create a new `tsconfig.json` that references the other two configs:
```json title="tsconfig.json"
{
"files": [],
"references": [
{ "path": "./tsconfig.src.json" },
{ "path": "./tsconfig.wasp.json" }
]
}
```
3. Create a `tsconfig.wasp.json` with the required compiler options and the Wasp Spec includes:
```json title="tsconfig.wasp.json"
{
"compilerOptions": {
"target": "ES2022",
"module": "esnext",
"moduleResolution": "bundler",
"jsx": "preserve",
"strict": true,
"isolatedModules": true,
"moduleDetection": "force",
"skipLibCheck": true,
"allowJs": true,
"noEmit": true,
"lib": ["ES2023"]
},
"include": ["**/*.wasp.ts", ".wasp/out/types/spec"]
}
```
4. Add the required `devDependencies` to your `package.json`:
```json title="package.json"
{
// ...
"devDependencies": {
// ...
"@types/node": "^24.0.0",
"@wasp.sh/spec": "file:.wasp/spec"
}
}
```
Keep your existing dependencies, and add these entries. `@types/node` is required because the Wasp Spec runs in a Node.js environment, and `@wasp.sh/spec` provides the local Wasp Spec API package.
5. Run `wasp install`.
6. Rename `main.wasp` to `main.wasp.old`.
7. Create a `main.wasp.ts` file with the following content:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec";
export default app({
name: "myApp",
title: "My app",
wasp: { version: "^0.24.0" },
head: [""],
spec: [
// ...
],
});
```
:::note
While previously we accepted any `*.wasp` file name, with the Wasp Spec the entry file must be named `main.wasp.ts`. You can still split the rest of your config across other `*.wasp.ts` files.
:::
8. Rewrite your config:
You can use the mapping above. Top-level concerns (e.g. `auth`, `server`, `client`, `db`, `emailSender`, `webSocket`) become keys of the `app({ ... })` object; pages, routes, queries, actions, APIs, jobs, and CRUDs go into the `spec` property.
9. Run your app with `wasp start`. If everything is correct, your app should behave exactly as before.
:::note
At some points, when the Spec needs to be regenerated, Wasp will tell you to run `wasp install` before being able to start the app. Usually, this might happen when upgrading Wasp versions, running `wasp clean`, or removing the `node_modules` folder.
:::
10. Delete `main.wasp.old` once you're sure the new config works.
See the full [Wasp Spec reference](https://wasp.sh/docs/general/spec#reference) for every option. Got stuck? Reach out on our [Discord](https://discord.gg/rzdnErX) and we'll help.
## Legacy / Wasp TS Config
:::note
Last checked with Wasp 0.24.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
The first version of configuring Wasp in TypeScript used a **class-based API**: you created an `App` instance with `new App(...)` and registered declarations with mutating method calls like `app.page(...)` and `app.query(...)`. We called this the **TS Config**.
Starting with Wasp 0.24, the TS Config is now retired in favor of the [Wasp Spec](https://wasp.sh/docs/general/spec): a **function-based API** where you call `app({ ... })` once and list everything in a `spec` property.
:::tip[Upgrading from Wasp 0.23 to 0.24?]
The conversion below is mechanical, so you can let an LLM do the heavy lifting instead. The [migration guide](https://wasp.sh/docs/0.24/migration-guide#use-an-agent-to-do-it-for-you) has a copyable prompt bundling this guide, the Wasp Spec docs, and the shared migration steps. Once your config is converted, return to the [migration guide](https://wasp.sh/docs/0.24/migration-guide) for the remaining shared steps.
:::
### New features
#### Reference imports
In the TS Config you could only reference your code with import objects (`{ import, from }`). The Wasp Spec also supports **reference imports**: import the value with the regular `import` syntax and pass it directly to a specification constructor.
**TS Config**
```ts title="main.wasp.ts"
const mainPage = app.page("MainPage", {
component: { importDefault: "MainPage", from: "@src/MainPage" },
});
app.query("getTasks", {
fn: { import: "getTasks", from: "@src/queries" },
});
```
**Wasp Spec**
```ts title="main.wasp.ts"
import MainPage from "./src/MainPage" with { type: "ref" };
import { getTasks } from "./src/queries" with { type: "ref" };
export default app({
// ...
spec: [
route("MainRoute", "/", page(MainPage)),
query(getTasks),
],
});
```
Import objects still work through the `ref(...)` helper, so you can migrate gradually. See the [Wasp Spec documentation](https://wasp.sh/docs/general/spec#referencing-your-apps-code) for the supported patterns and their limitations.
#### Multiple files
The TS Config required your entire configuration to live in a single `main.wasp.ts`. The Wasp Spec lets you split it across multiple `*.wasp.ts` files and import specifications between them, so you can keep large apps organized (for example, a separate `auth.wasp.ts` or `cards.wasp.ts` next to the feature it configures).
See the [Wasp Spec documentation](https://wasp.sh/docs/general/spec#splitting-your-spec-into-multiple-files) for details.
### Changes
#### Overview
| What | Before | After |
| ------------------- | ------------------------------------------------------------------------------------------------------------------- | ---------------------------------- |
| Creating an app | `new App(name, { ... });` | `app({ name, ..., spec: [...] });` |
| Configuring the app | `app.auth(...);` `app.server(...);` `app.client(...);` `app.db(...);` `app.emailSender(...);` `app.webSocket(...);` | \`\`\` |
| app({ | | |
| auth: ..., | | |
| server: ..., | | |
| client: ..., | | |
| db: ..., | | |
| emailSender: ..., | | |
| webSocket: ..., | | |
| }); | | |
````|
| Adding app specifications | `app.route(...);` `app.query(...);` `app.action(...);` etc | ```
app({
spec: [
route(...),
query(...),
action(...),
]
});
``` |
| Imports | `{ import, from }` | `import { ... } from "./src/..." with { type: "ref" };` |
| Package name | `wasp-config` | `@wasp.sh/spec` |
### App and specifications
**TS Config**
```ts title="main.wasp.ts"
import { App } from "wasp-config";
const app = new App("todoApp", {
title: "ToDo App",
wasp: { version: "^0.24.0" },
});
const mainPage = app.page("MainPage", {
component: { importDefault: "MainPage", from: "@src/MainPage" },
});
app.route("MainRoute", { path: "/", to: mainPage });
app.query("getTasks", {
fn: { import: "getTasks", from: "@src/queries" },
entities: ["Task"],
});
export default app;
````
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, page, query, route } from "@wasp.sh/spec";
import MainPage from "./src/MainPage" with { type: "ref" };
import { getTasks } from "./src/queries" with { type: "ref" };
export default app({
name: "todoApp",
title: "ToDo App",
wasp: { version: "^0.24.0" },
spec: [
route("MainRoute", "/", page(MainPage)),
query(getTasks, { entities: ["Task"] }),
],
});
```
#### API: `httpRoute` becomes positional arguments
**TS Config**
```ts title="main.wasp.ts"
app.apiNamespace("bar", {
middlewareConfigFn: { import: "barNamespaceMiddlewareFn", from: "@src/apis" },
path: "/bar",
});
app.api("barBaz", {
fn: { import: "barBaz", from: "@src/apis" },
auth: false,
entities: ["Task"],
httpRoute: { method: "GET", route: "/bar/baz" },
});
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { api, apiNamespace, app } from "@wasp.sh/spec";
import { barBaz, barNamespaceMiddlewareFn } from "./src/apis" with { type: "ref" };
export default app({
// ...
spec: [
apiNamespace("/bar", {
middlewareConfigFn: barNamespaceMiddlewareFn,
}),
api("GET", "/bar/baz", barBaz, { auth: false, entities: ["Task"] }),
],
});
```
#### Jobs: `perform` is flattened
**TS Config**
```ts title="main.wasp.ts"
app.job("mySpecialJob", {
executor: "PgBoss",
perform: {
fn: { import: "foo", from: "@src/jobs/bar" },
executorOptions: { pgBoss: { retryLimit: 1 } },
},
entities: ["Task"],
});
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, job } from "@wasp.sh/spec";
import { foo } from "./src/jobs/bar" with { type: "ref" };
export default app({
// ...
spec: [
job(foo, {
executor: "PgBoss",
entities: ["Task"],
performExecutorOptions: { pgBoss: { retryLimit: 1 } },
}),
],
});
```
#### CRUD
**TS Config**
```ts title="main.wasp.ts"
app.crud("tasks", {
entity: "Task",
operations: {
getAll: {},
create: { overrideFn: { import: "createTask", from: "@src/actions" } },
},
});
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app, crud } from "@wasp.sh/spec";
import { createTask } from "./src/actions" with { type: "ref" };
export default app({
// ...
spec: [
crud("tasks", "Task", {
getAll: {},
create: { overrideFn: createTask },
}),
],
});
```
#### Top-level config: `auth`, `server`, `client`, `db`, `emailSender`, `webSocket`
These were configured with mutating method calls. They are now keys of the `app({ ... })` object.
**TS Config**
```ts title="main.wasp.ts"
const app = new App("todoApp", {
title: "ToDo App",
wasp: { version: "^0.24.0" },
});
app.auth({
userEntity: "User",
methods: { google: {} },
onAuthFailedRedirectTo: "/login",
});
app.client({
rootComponent: { importDefault: "App", from: "@src/App" },
});
app.emailSender({
provider: "SMTP",
defaultFrom: { email: "hi@example.com" },
});
export default app;
```
**Wasp Spec**
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec";
import App from "./src/App" with { type: "ref" };
export default app({
name: "todoApp",
title: "ToDo App",
wasp: { version: "^0.24.0" },
auth: {
userEntity: "User",
methods: { google: {} },
onAuthFailedRedirectTo: "/login",
},
client: {
rootComponent: App,
},
emailSender: {
provider: "SMTP",
defaultFrom: { email: "hi@example.com" },
},
// ...
});
```
### How to migrate
These steps convert an old class-based Wasp TS Config to the new Wasp Spec. Before running `wasp install` below, make sure your app's Wasp version is `^0.24.0`.
After finishing this guide, return to the [migration guide](https://wasp.sh/docs/0.24/migration-guide) if you still need to complete the shared Wasp 0.24 migration steps.
Wasp validates the Wasp Spec support files during migration, including the required `package.json` entries, `tsconfig.wasp.json` options, and `tsconfig.src.json` exclusions.
1. Update your `package.json` with the new dependencies:
**Before**
```json title="package.json"
{
// ...
"devDependencies": {
// ...
"wasp-config": "file:.wasp/wasp-config"
}
}
```
**After**
```json title="package.json"
{
// ...
"devDependencies": {
// ...
"@types/node": "^24.0.0",
"@wasp.sh/spec": "file:.wasp/spec"
}
}
```
Keep your existing dependencies, replace `wasp-config` with `@wasp.sh/spec`, and add `@types/node`. `@types/node` is required because the Wasp Spec runs in a Node.js environment.
2. Update your `tsconfig.wasp.json` and make sure it includes the following settings:
```json title="tsconfig.wasp.json"
{
"compilerOptions": {
"target": "ES2022",
"module": "esnext",
"moduleResolution": "bundler",
"jsx": "preserve",
"strict": true,
"isolatedModules": true,
"moduleDetection": "force",
"skipLibCheck": true,
"allowJs": true,
"noEmit": true,
"lib": ["ES2023"]
},
"include": ["**/*.wasp.ts", ".wasp/out/types/spec"]
}
```
3. Make sure your `tsconfig.src.json` excludes Wasp Spec files:
```json title="tsconfig.src.json"
{
// ...
"include": ["src"],
"exclude": ["**/*.wasp.ts"]
}
```
4. Run `wasp install`.
5. Rewrite `main.wasp.ts`:
Replace `new App(...)` and the `app.*(...)` method calls with a single `app({ ... })` call whose `spec` property holds the specifications (see the [mapping above](#changes)), and update the import:
**Before**
```ts title="main.wasp.ts"
import { App } from "wasp-config";
const app = new App("myApp", {
title: "My app",
wasp: { version: "^0.24.0" },
});
```
**After**
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec";
export default app({
name: "myApp",
title: "My app",
wasp: { version: "^0.24.0" },
head: [""],
spec: [
// ...
]
});
```
:::note
While previously we accepted any `*.wasp.ts` file name, with the Wasp Spec the entry file must be named `main.wasp.ts`. You can still split the rest of your config across other `*.wasp.ts` files.
:::
6. Run your app with `wasp start`. If everything is correct, your app should behave exactly as before.
:::note
At some points, when the Spec needs to be regenerated, Wasp will tell you to run `wasp install` before being able to start the app. Usually, this might happen when upgrading Wasp versions, running `wasp clean`, or removing the `node_modules` folder.
:::
See the full [Wasp Spec reference](https://wasp.sh/docs/general/spec#reference) for every option. Got stuck? Reach out on our [Discord](https://discord.gg/rzdnErX) and we'll help.
## Libraries / Radix Themes
:::note
Last checked with Wasp 0.24 and Radix Themes 3.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to integrate the [Radix Themes](https://www.radix-ui.com/themes) component library into your Wasp application.
### Setting up Radix Themes
#### 1. Install Radix Themes
Install the Radix Themes package:
```bash
npm install @radix-ui/themes
```
#### 2. Create a Root Component if it doesn't exist
Due to how Radix works, we'll need to have a single component that wraps all the pages in our app. In Wasp, this is done through the `rootComponent` configuration:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
import { Layout } from "./src/Layout" with { type: "ref" }
export default app({
name: "MyApp",
wasp: { version: "^0.24.0" },
title: "My App",
head: [""],
client: {
rootComponent: Layout,
},
// ...
})
```
If you already have a root component in your Wasp app, open that file and skip to the next step. If you don't have one, create a new file with an empty component that will serve as the root:
```tsx title="src/Layout.tsx"
import type { ReactNode } from "react";
export function Layout({ children }: { children?: ReactNode }) {
return children;
}
```
#### 3. Add Radix Themes to your root component
In your root component, we'll wrap the `children` with Radix Theme's `Theme` component, and import their CSS stylesheet:
```tsx title="src/Layout.tsx"
import type { ReactNode } from "react";
import "@radix-ui/themes/styles.css";
import { Theme } from "@radix-ui/themes";
export function Layout({ children }: { children?: ReactNode }) {
return {children};
}
```
#### 4. Use Radix Themes components
Now you can use Radix Themes components anywhere in your application:
```tsx title="src/MainPage.tsx"
import { Flex, Text, Button } from "@radix-ui/themes";
export const MainPage = () => {
return (
Hello from Radix Themes :)
);
};
```
That's it!
### Customizing the theme
You can customize the theme by passing props to the `Theme` component:
```tsx title="src/Layout.tsx"
import type { ReactNode } from "react";
import "@radix-ui/themes/styles.css";
import { Theme } from "@radix-ui/themes";
export function Layout({ children }: { children?: ReactNode }) {
return (
{children}
);
}
```
See the [Radix Themes documentation](https://www.radix-ui.com/themes/docs/overview/getting-started) for more customization options.
## Libraries / Shadcn
:::note
Last checked with Wasp 0.24 and Shadcn (as of May 28, 2026).
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
### Setting up Shadcn in a Wasp project
We'll be loosely following [the Vite instructions for Shadcn](https://ui.shadcn.com/docs/installation/vite) since Wasp is using Vite + React. Some of the steps don't apply, so we've adjusted them accordingly.
You won't be able to use the `@` alias setup since it's not currently supported by Wasp. Because of this you'll need to adjust some imports when we generate components, but it should be fairly straightforward to do.
#### 1. Add Tailwind CSS
If you haven't added Tailwind CSS to your Wasp project yet, follow the instructions in the [Tailwind CSS guide](https://wasp.sh/docs/guides/libraries/tailwind) first.
#### 2. Temporarily set up the `@` alias
We need to temporarily setup the `@` alias to pass Shadcn's "Preflight checks". We'll remove it later.
Add a top-level `compilerOptions` block to your `tsconfig.json`:
```diff title="tsconfig.json"
{
+ "compilerOptions": {
+ "baseUrl": ".",
+ "paths": {
+ "@/*": ["./src/*"]
+ }
+ },
"files": [],
"references": [
{ "path": "./tsconfig.src.json" },
{ "path": "./tsconfig.wasp.json" }
],
}
```
#### 3. Setup Shadcn
Go into your project directory and run:
```bash
npx shadcn@latest init -b radix -p luma
```
This initializes Shadcn with the Radix component library and the Luma preset. You should see output like this:
```bash
โ Preflight checks.
โ Verifying framework. Found Vite.
โ Validating Tailwind CSS. Found v4.
โ Validating import alias.
โ Writing components.json.
โ Checking registry.
โ Installing dependencies.
โ Updating src/Main.css
โ Created 1 file:
- src/lib/utils.ts
```
#### 4. Remove the `@` alias
Remove the lines we added in the `tsconfig.json`:
```diff title="tsconfig.json"
{
- "compilerOptions": {
- "baseUrl": ".",
- "paths": {
- "@/*": ["./src/*"]
- }
- },
"files": [],
"references": [
{ "path": "./tsconfig.src.json" },
{ "path": "./tsconfig.wasp.json" }
],
}
```
#### 5. Adjust the `components.json`
Adjust the `aliases` in `components.json` to be:
```json title="components.json"
{
"$schema": "https://ui.shadcn.com/schema.json",
// ...
"aliases": {
"components": "src/components",
"utils": "src/lib/utils",
"ui": "src/components/ui",
"lib": "src/lib",
"hooks": "src/hooks"
},
}
```
### Adding a component
In this example, we'll add the `Button` component.
#### 1. Use the `shadcn` CLI to add the component
We'll add a button component with:
```bash
npx shadcn@latest add button
```
#### 2. Adjust the `utils` import
You'll notice that you now have a brand new `button.tsx` file in `src/components/ui`. We need to fix some import issues:
```diff title="src/components/ui/button.tsx"
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { Slot } from "radix-ui"
-import { cn } from "@/lib/utils"
+import { cn } from "../../lib/utils"
```
#### 3. Use the `Button` component
That's it, now you are ready to use the `Button` component!
```tsx title="src/MainPage.tsx"
import "./Main.css";
import { Button } from "./components/ui/button";
export const MainPage = () => {
return (
);
};
```
## Libraries / Tailwind CSS
:::note
Last checked with Wasp 0.24 and Tailwind 4.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
Wasp works great with [Tailwind CSS](https://tailwindcss.com/), a utility-first CSS framework. You can use Tailwind CSS by setting it up through their [Vite installation method](https://tailwindcss.com/docs/installation/using-vite), as with any other project.
### Adding Tailwind to your Wasp project
1. Install Tailwind and its Vite plugin.
```bash
npm install tailwindcss
npm install -D @tailwindcss/vite
```
2. Add the Tailwind CSS Vite plugin to your `vite.config.ts` file:
```ts title="vite.config.ts"
import { wasp } from 'wasp/client/vite'
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
wasp(),
tailwindcss()
],
server: {
open: true,
},
})
```
3. Import Tailwind into your base CSS file. For example, in a project created with `wasp new` you might import Tailwind into `Main.css`.
```css title="src/Main.css"
@import "tailwindcss";
/* ... */
```
4. Start using Tailwind ๐ฅณ
```tsx title="src/MainPage.tsx"
// ...
Hello world!
;
// ...
```
### Adding Tailwind Plugins
Wasp doesn't require any special configuration to use Tailwind plugins. You can follow each plugin's installation instructions as you normally would.
For example, to add the [Tailwind Forms](https://github.com/tailwindlabs/tailwindcss-forms) and [Tailwind Typography](https://github.com/tailwindlabs/tailwindcss-typography) plugins, we can check the installation instructions on their respective documentation pages and follow them as usual:
```shell
npm install -D @tailwindcss/forms
npm install -D @tailwindcss/typography
```
```css title="src/Main.css"
@import "tailwindcss";
@plugin "@tailwindcss/forms";
@plugin "@tailwindcss/typography";
/* ... */
```
## Optimization / Meta tags
:::note
Last checked with Wasp 0.24.
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
This guide shows you how to set up meta tags for your Wasp application to improve SEO and enable rich previews when your app is shared on platforms like Slack, X, or Discord.
### How to add `` tags
#### Setting metadata for every page
You can add meta tags to your application using the `head` property in your `app` spec. These tags will be included in the `` section of your HTML.
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
name: "MyApp",
wasp: { version: "^0.24.0" },
title: "My App",
head: [
"",
"",
"",
"",
// Open Graph tags for social media previews
"",
"",
"",
"",
"",
"",
// Twitter Card tags
"",
"",
"",
"",
],
// ...
})
```
#### Setting metadata for a specific page
You can use [React's support for `` tags](https://react.dev/reference/react-dom/components/meta) within components to set metadata for specific pages. This allows you to customize the metadata based on the content of each page. These tags will only be included in the page when the component is being shown, so it's better to add them to the top-most level if possible.
```tsx title="src/pages/HomePage.tsx"
export function HomePage() {
return (
<>
Welcome to the Home Page
{/* The rest of the page content */}
>
);
}
```
### Recommended `` Tags
#### Basic SEO tags
- `description`: A brief description of your app (appears in search results)
- `author`: The creator or company name
- `keywords`: Relevant keywords for search engines
#### Open Graph tags
This is the most common standard used by social media platforms (e.g. Facebook, LinkedIn, Slack, Discord, and more) to generate rich link previews.
- `og:type`: Usually "website" for web apps
- `og:title`: The title shown in previews
- `og:site_name`: Your app/site name
- `og:url`: The canonical URL
- `og:description`: Description for the preview
- `og:image`: Preview image URL
You can check [Open Graph tag guidelines](https://ogp.me/) for more information on how this information is used.
#### X Card tags
This is used by X (formerly Twitter) to create rich link previews.
- `twitter:card`: Use "summary\_large\_image" for large image previews
- `twitter:image`: Image URL for Twitter previews
- `twitter:image:width`: Image width in pixels
- `twitter:image:height`: Image height in pixels
X falls back to the Open Graph tags for the title, description, and image, so if you've already set those, the only tag you strictly need to add is `twitter:card`.
You can check [X's guidelines](https://developer.x.com/en/docs/x-for-websites/cards/overview/markup) for more information on how this information is used.
#### Canonical URL
- ``: The "original" URL of the page, without tracking parameters (like `?utm_source`). Adding a self-referencing canonical link to each page prevents crawlers from indexing parameterized variations of the same page as duplicates. Since it's different for every page, set it per-page rather than in the `head` field.
### Best practices for images
1. Use your client app's absolute URL (including `https://`) for your preview images.
2. Check the recommended dimensions for each platform's images in their documentation.
3. Keep important content centered (some platforms crop differently).
4. Use WebP or PNG format for best quality.
5. Place your image [in the `public/` folder](https://wasp.sh/docs/project/static-assets#the-public-directory).
### Testing your metadata
After deploying, you can verify your meta tags using these tools:
- [Google Tag Assistant](https://tagassistant.google.com/)
- [Facebook Sharing Debugger](https://developers.facebook.com/tools/debug/)
- [X Card Validator](https://cards-dev.x.com/validator)
- [LinkedIn Post Inspector](https://www.linkedin.com/post-inspector/)
## Optimization / SEO & GEO
:::note
Last checked with Wasp 0.24, Lighthouse 13, and industry standards (as of Jun 8, 2026).
This guide depends on external libraries or services, so it may become outdated over time. We do our best to keep it up to date, but make sure to check their documentation for any changes.
:::
Search engine optimization (SEO) and generative engine optimization (GEO) are about making your app visible and attractive to search engines, social media platforms, and AI assistants.
### Where to optimize
**In general, we recommend applying the following optimization techniques to your content pages, not your app pages.** Think about which pages you want to be surfaced to users, that provide good information *about* your app to a wide audience.
- **Content pages** include your landing page, about page, pricing page, and other marketing pages. These are the pages you want to show up in search results and link previews. They usually don't change much often, nor based on who visits them. **SEO is most effective for these.**
- **App pages** include your dashboard, profile page, settings page, a form, chat interface, or any page that shows dynamic content based on user data. These pages should be hidden from other users, as they contain personalized data. They also can't be meaningfully indexed, since their main goal is to be interacted with, not read.
You can use this broad distinction to decide which pages you apply SEO techniques to. For example, you definitely want ChatGPT to be able to read your landing page so it can recommend your site to users, but you definitely *don't* want Google to recommend some user's specific dashboard page to other users.
### Measuring your SEO {#measuring}
We recommend **first to measure your app** against common industry tools and see where you stand. Then, you can pick the techniques that are most relevant to your app and focus on those. After applying them, measure again to see how much they improved your score, and if there are any new issues to fix.
SEO can come with costs; at a minimum, the cost of development time and maintenance burden. So while there's always room for improvement, it's important to focus on the techniques that will give you the biggest boost. A little improvement can go a long way, and you don't need to get a perfect score to see significant benefits.
:::warning[Always run measurements against your production build]
Full optimization of the Wasp app only happens on the production build, not the development server. Running Lighthouse or other tools against `wasp start` won't reflect what crawlers and users actually get. Always run it against your production build, either [locally](https://wasp.sh/docs/deployment/local-testing) or [after deploying](https://wasp.sh/docs/deployment/intro).
:::
#### Lighthouse
[Lighthouse](https://developer.chrome.com/docs/lighthouse/overview) should be the first tool you use to measure your website's readiness for search engines and AI assistants. It will score your page on a number issues and give you a neat list of which ones you need to fix, ordered by importance.
| | |
| -------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
|  |  |
There are four main categories of issues (Performance, Accessibility, Best Practices, and SEO), and all of these will be important for how your pages perform in search engines and AI assistants. For example, Google uses page speed as a ranking factor, and AI assistants will be more likely to read and recommend your site if it's accessible.
A straightforward way to run Lighthouse is through the command line:
```sh
# First, build and start the production build of your app:
$ wasp build
$ wasp build start
# While the server is running, open another terminal and run Lighthouse:
$ npx lighthouse http://localhost:3000 --preset=desktop --view
# You can change the URL to a specific page or to point to your deployed app if you want to test that instead.
# Remove the --preset=desktop argument to test the mobile experience instead.
```
It will take a minute to run (you'll see an automated browser window while it runs), and then it will save your report as an HTML file and open it.
Using the Lighthouse [command-line interface](https://github.com/GoogleChrome/lighthouse#using-the-node-cli) as we just did is a good way to run it regularly during development, and let your AI assistant read the report and fix issues. You can also run it directly inside the [Chrome DevTools](https://github.com/GoogleChrome/lighthouse#using-lighthouse-in-chrome-devtools), or through the [online service](https://pagespeed.web.dev/) (but it will only work for deployed apps).
#### Search Console
[Google Search Console](https://search.google.com/search-console) is a free service from Google that shows you how your deployed site appears in Google Search, which queries show it, and how many clicks it gets.
It will also show you any issues it finds when crawling your site, and guide you through fixing them. It's a must-have for monitoring your SEO performance and catching any issues early on.
#### Single-purpose tools
Some SEO techniques have specific tools available to check if they're implemented correctly. Inside the links for each technique below, you will find a mention to that tool if it exists, and we recommend using it to check your implementation.
### Optimization techniques
Good SEO comes down to a few separate problems. We ordered them here by their effort-impact ratio, so you can focus on the techniques that will give you a bigger boost for less effort first. These are:
- Telling crawlers what each page is about, with [meta tags](#meta-tags).
- Making your content visible without running JavaScript, with [prerendering](#prerendering).
- Helping crawlers understand your content with [semantic markup](#semantic-markup) and [structured data](#structured-data).
- Making your pages [smaller](#reduce-size).
- Giving crawlers the [standard files](#well-known-files) they look for.
These are general techniques that apply to most websites, but there are many more you can use depending on your specific app and goals. You can start with these, and explore more as needed. Keep in mind that these techniques can only amplify your content, not replace it; we talk more about that [below](#good-content).
#### Meta tags
Search engines and social platforms read `` tags from your HTML to decide what to show in results and link previews: the page title, its description, and the preview image.
You can set tags for your whole app through the `head` field of your `app` declaration:
```ts title="main.wasp.ts"
import { app } from "@wasp.sh/spec"
export default app({
// ...
title: "My App",
head: [
"",
"",
"",
"",
],
// ...
})
```
For tags that change from page to page, or depend on dynamic data, you can render `` tags directly inside a page component using [React's support for `` tags](https://react.dev/reference/react-dom/components/meta):
```tsx title="src/pages/ProductPage.tsx"
export function ProductPage({ productId }) {
const product = useProduct(productId);
return (
<>
{/* Twitter/X falls back to the og: tags for everything else;
this tag enables the large preview layout */}
{/* The "original" URL of this page, without tracking parameters,
so crawlers don't index duplicate variations of it */}
{product.name}
{product.description}
{/* ... */}
>
);
}
```
Read more at our dedicated `` tags guide:
[Guide](https://wasp.sh/docs/guides/optimization/meta-tags)
#### [Meta tags ยป](https://wasp.sh/docs/guides/optimization/meta-tags)
[The full set of recommended tags, image guidelines, and testing tools.](https://wasp.sh/docs/guides/optimization/meta-tags)
#### Prerendering
By default, Wasp apps are [single-page applications (SPAs)](https://en.wikipedia.org/wiki/Single-page_application), so you get fast navigation and responsive interactions. In an SPA, the HTML files your browser downloads are mostly empty and have just enough to load the JavaScript code that actually powers the app. Browsers execute the JavaScript to show you content.
Most search engines (like Google) can also execute JavaScript when indexing a page too. But some crawlers (and many AI assistants) don't run JavaScript at all. So they see only the empty HTML, not your actual content. As such, they can't answer questions about your website, and they'll mostly ignore it.

However, Wasp can **prerender** chosen routes to static HTML at build time, so the content is readable in the initial file even without JavaScript. Browsers will still download and execute the rest of the app, and present the same experience as with a pure SPA, so you get the best of both worlds. You can opt-in per route:
```ts title="main.wasp.ts"
import { app, page, route } from "@wasp.sh/spec"
import { LandingPage } from "./src/LandingPage" with { type: "ref" }
export default app({
// ...
decls: [
route("LandingRoute", "/", page(LandingPage), {
prerender: true
}),
],
})
```
Prerendering can't be used on routes with dynamic paths or on auth-required pages. You can read more in our prerendering documentation:
[Documentation](https://wasp.sh/docs/advanced/prerendering)
#### [Prerendering ยป](https://wasp.sh/docs/advanced/prerendering)
[How it works, when to use it, and how to avoid hydration mismatches.](https://wasp.sh/docs/advanced/prerendering)
#### Semantic markup
Most crawlers and screen readers can't see your inside images, so every meaningful image needs a text description through its `alt` attribute. Missing `alt` text is one of the most common issues a Lighthouse SEO audit flags.
```tsx title="src/components/Testimonials.tsx"
;
```
If an image is purely decorative, you can give it an empty `alt=""` so crawlers know to ignore it. But if the image conveys information, like a product photo or a profile picture, the `alt` text should describe that information.
You should also use semantic HTML to help crawlers understand your content. For example, use one `
` per page for the main heading, and use `
`, `
`, etc. for subheadings in order. Most indexers will understand that as your page's subject matter and closely relate it with those terms. You should also use descriptive link text instead of generic phrases like "click here," so crawlers know what the linked page is about.
Links deserve special attention, since crawlers discover your pages by following `` tags. A `