# Multi-tenant SaaS mode with WorkOS

> Run your own multi-tenant pushq with WorkOS sign-in, where anyone can sign up, create teams and get isolated queues, tasks and API keys.

Source: https://pushqueue.dev/docs/self-hosting/saas-mode/

By default a self-hosted pushq server has one team: you and the people you invite, all sharing the same queues. **SaaS mode** turns it into a multi-tenant service like the hosted one at pushqueue.dev: anyone can sign up, create teams, and get their own isolated queues, tasks, stats and API keys. Sign-in and team membership are handled by [WorkOS](https://workos.com/).

Use this when you want to offer pushq to several customers or internal groups from one deployment, with each one unable to see the others' data. For a single team, the default mode is simpler.

## What changes

| | Self-hosted (default) | SaaS mode |
|---|---|---|
| Turned on by | Nothing; no WorkOS settings | `WORKOS_CLIENT_ID` and `WORKOS_API_KEY` |
| Sign-in | Local accounts with passwords; the first person creates the admin | A 6-digit code emailed by WorkOS, or GitHub, on pushq's own sign-in page. Signing in the first time creates the account. No passwords. |
| Teams | One: everyone on the server | Anyone can create teams; a person can belong to several and switch between them |
| Data | Shared by everyone | Isolated per team: queues, tasks, attempts, stats and API keys |
| Invites | A one-time link you send yourself | Emailed by WorkOS |
| Config-file `api_keys` | Ordinary admin/writer keys | Operator keys that see every team |

Everything in [Team members](https://pushqueue.dev/docs/guides/team-members/) that's described for the hosted service applies to SaaS mode.

## Turn it on

Set both WorkOS credentials and start the server with `pushq serve`:

```bash
export WORKOS_CLIENT_ID=client_...
export WORKOS_API_KEY=sk_...
export PUSHQ_DB=postgres://pushq:secret@db.internal/pushq
export PUSHQ_API_KEYS="admin:pq_operator_<generate>"
export PUSHQ_METRICS_PUBLIC=false
pushq serve
```

The log confirms it at startup:

```text
level=INFO msg="SaaS mode: WorkOS sign-in, one isolated workspace per team" client_id=client_... redirect_uri=<origin>/v1/auth/workos/callback
```

| Variable | YAML key | Default | Description |
|---|---|---|---|
| `WORKOS_CLIENT_ID` | `workos.client_id` | none | Your WorkOS client ID. Required for SaaS mode. |
| `WORKOS_API_KEY` | `workos.api_key` | none | Your WorkOS API key. Required for SaaS mode. Prefer the environment variable over writing it in the config file. |
| `WORKOS_REDIRECT_URI` | `workos.redirect_uri` | `<request origin>/v1/auth/workos/callback` | Where WorkOS sends people back after GitHub sign-in. Set it when pushq is behind a proxy that changes the host or scheme. It must be listed under Redirects in WorkOS. |

Set both `WORKOS_CLIENT_ID` and `WORKOS_API_KEY`, or neither: with only one, the server refuses to start. With neither, it runs in the default self-hosted mode.

pushq also reads a `.env` file from the directory where `pushq serve` runs (or the file named by `PUSHQ_ENV_FILE`). Variables already set in the real environment win over the file.

```bash
# .env (keep it out of version control)
WORKOS_CLIENT_ID=client_...
WORKOS_API_KEY=sk_...
# WORKOS_REDIRECT_URI=https://pushq.example.com/v1/auth/workos/callback
PUSHQ_API_KEYS=admin:pq_operator_<generate>
```

The equivalent in the YAML config file:

```yaml
workos:
  client_id: client_...
  redirect_uri: https://pushq.example.com/v1/auth/workos/callback
# api_key: better passed as WORKOS_API_KEY
```

SaaS mode works with SQLite and Postgres. `pushq dev` ignores WorkOS settings and always runs with authentication off.

Serve pushq over HTTPS. Behind a TLS-terminating proxy, forward `X-Forwarded-Proto: https` so session cookies are marked `Secure` and the default redirect URI uses `https`. See [Deploy](https://pushqueue.dev/docs/self-hosting/deploy/).

## WorkOS dashboard checklist

1. **Authentication:** enable **Magic Auth** (email codes) and **GitHub OAuth**, and allow sign-ups so new people can create accounts.
   - **Email verification:** with it on, WorkOS pauses a GitHub sign-in until the person enters a code it emails them; pushq's sign-in page asks for it. Turn it off to let GitHub sign-ins straight in, in which case pushq trusts the email address GitHub provides. Email-code sign-ins prove the address either way.
2. **Redirects → Sign-in callback** (used by GitHub sign-in only): add `https://<your-pushq-host>/v1/auth/workos/callback`. For local testing against a staging environment, `http://localhost:8080/v1/auth/workos/callback`. If you set `WORKOS_REDIRECT_URI`, add exactly that URL.
3. **Roles:** keep the default `admin` and `member` roles. pushq uses those slugs; if `admin` is missing, it falls back to the environment's default role.
4. Use the WorkOS **staging** environment while testing.

## How people sign in

The sign-in page is pushq's own (served by the dashboard at `/login`); WorkOS works behind it and no WorkOS-hosted screens are shown, apart from GitHub's consent screen.

- **Email code:** the person enters their email and pushq asks WorkOS to send a 6-digit code. An email WorkOS hasn't seen before gets a new WorkOS user first, so this is also sign-up. The response never reveals whether the email already had an account.
- **GitHub:** the browser goes to GitHub through WorkOS and comes back to `/v1/auth/workos/callback`.

After sign-in, pushq finds or creates the local account (matching by WorkOS user, then by verified email), mirrors the person's WorkOS organizations as teams, and starts the session in the team they used last. Someone with no team yet lands on the **Welcome** screen to create one.

Password sign-in, first-time setup, invite and reset links, and password changes are switched off: those endpoints return `404` (`/v1/auth/setup`, `/v1/auth/login`, `/v1/auth/invites/{token}`, `/v1/auth/invites/{token}:accept`, `/v1/me/password`, `/v1/users/{id}/reset-link`).

## Teams and roles

**Each team is a WorkOS Organization.** Creating a team in the dashboard creates an organization with the creator as its `admin` member. Renaming the team renames the organization. Inviting someone sends a WorkOS invitation email; removing someone deletes their organization membership.

**Roles live in pushq.** pushq has three roles per team (admin, member, viewer; see [Roles](https://pushqueue.dev/docs/guides/team-members/#roles)), and WorkOS has two. pushq maps them like this:

| pushq role | WorkOS organization role |
|---|---|
| admin | `admin` |
| member | `member` |
| viewer | `member` (pushq enforces read-only) |

pushq's role is authoritative. When it learns of a member from WorkOS (someone added in the WorkOS dashboard, for example), it gives them `admin` if their WorkOS role is `admin` and `member` otherwise, unless a pending pushq invite for their email says which role to grant. After that, changing their role in WorkOS doesn't change it in pushq; change it on pushq's Team page.

**pushq re-syncs from WorkOS** every time a person signs in (their teams) and whenever a team's Team settings page is opened (its members). Memberships added or removed directly in the WorkOS dashboard are picked up then.

## How team data is isolated

- Queues, tasks, attempts, stats and API keys all belong to a team. Two teams can each have a queue called `emails`.
- Internally each team gets a random 12-character namespace, and its queues are stored as `<namespace>:<name>`. Queue names can't contain `:`, so no name a client sends can reach outside its own namespace. pushq strips the namespace from everything it returns, including error messages.
- Receivers only ever see the short name: the `X-Pushq-Queue` header and the signed `webhook-id` use `emails`, not the stored name.
- API keys created in the dashboard or through `/v1/api-keys` belong to the team that created them and only see its queues. A team's admin key manages that team's members, invites and keys.
- A person removed from a team loses access at once; their sessions on that team stop working on the next request.

## Operator keys

In SaaS mode, the keys in the config file (`api_keys` or `PUSHQ_API_KEYS`) are **operator keys** for you, the operator:

- They aren't tied to any team and see every team's queues. Listings show stored names, `<namespace>:<name>`.
- Teams never see them: the API keys page reports no config keys.
- Only operator keys can read `/metrics` when it isn't public (see below).
- Queues created with an operator key belong to no team.

Keep them for your own tooling and monitoring. Team management (members, invites, team API keys) belongs to each team's admins, in the dashboard or with that team's admin key; don't use operator keys for it.

### Metrics

`/metrics` is public by default (`metrics.public: true`). Its metrics are aggregate over the whole instance (no per-queue or per-team labels), but you likely don't want customers reading your instance's totals. Set `PUSHQ_METRICS_PUBLIC=false` (or `metrics: { public: false }`) and scrape with an operator key:

```bash
curl https://pushq.example.com/metrics \
  -H "Authorization: Bearer $PUSHQ_OPERATOR_KEY"
```

## Switch an existing server to SaaS mode

You can add the WorkOS settings to a server that already has data. What happens to it:

- **Queues and tasks** are left untouched, but they belong to no team. Only operator keys can see and manage them, under their existing names.
- **Dashboard accounts** are kept. When someone signs in through WorkOS with the same verified email, pushq links their existing account instead of creating a new one. Their password and old server-wide role no longer apply; they start with no team until they create one or accept an invite.
- **API keys created in the dashboard** before the switch stop working: they belong to no team, and pushq refuses them in SaaS mode. Create new keys inside a team.
- **Config-file keys** keep working and become operator keys.
- **Pending invite and password-reset links** stop working.

Plan the switch like a migration: create the teams, move workloads to queues created with each team's own keys, then update your apps to use those keys.
