> ## Documentation Index
> Fetch the complete documentation index at: https://docs.masivo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate with OAuth

> Authenticate servers and apps with scoped OAuth tokens instead of API keys

Masivo issues short-lived OAuth 2.0 tokens (1 hour) with least-privilege
access. There are two kinds of credentials, one per kind of caller:

| You are building | Use | Endpoint | The token reaches |
| - | - | - | - |
| A backend / server | **OAuth client** | `POST /api/storefront/v1/oauth/token` | Any customer, limited by **scopes** |
| A web or mobile front end | **OAuth app** | `POST /api/storefront/v1/oauth/exchange` | **Only the signed-in customer** |

Both are created in **Settings → OAuth** and belong to an
[environment](https://docs.masivo.ai/guides/settings/environments.md). Send the returned
`access_token` as `Authorization: Bearer <token>` on every Storefront request.

> [!WARNING]
>
> Never ship an OAuth client secret inside a front end. Apps use the token
> exchange, which needs no secret.

***

## OAuth clients (servers)

**1. Create the client**

Go to **Settings → OAuth → Clients → New client**. Pick an environment, a
name and the scopes the integration needs (for example `customers:read`
and `events:write`).

**2. Copy the credentials**

Copy the `client_id` (`mv_…`) and the `client_secret` (`mvs_…`). The secret
is shown **only once**; Masivo only stores its hash. Use **Rotate secret**
if it leaks.

**3. Request a token**

Use the client credentials grant:

```bash theme={null}
curl -X POST "https://app.masivo.ai/api/storefront/v1/oauth/token" \
  -d grant_type=client_credentials \
  -d client_id=$CLIENT_ID \
  -d client_secret=$CLIENT_SECRET \
  -d "scope=customers:read events:write"
```

```json theme={null}
{
  "access_token": "eyJ...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "customers:read events:write"
}
```

Omit `scope` to get every scope of the client. Asking for a scope the client
doesn't have fails with `invalid_scope`.

### Scopes

Scopes follow `<resource>:read|write`. `read` covers `GET` and `HEAD`; `write`
covers every other method. The resource comes from the first path segment
after `/api/storefront/v1/`. A path missing from this table is denied to every
OAuth token.

| Resource | Path segments |
| - | - |
| `customers` | `customers` |
| `contacts` | `contacts` |
| `events` | `behavior`, `events` |
| `transactions` | `transactions` |
| `coupons` | `coupons` |
| `rewards` | `rewards` |
| `tiers` | `tiers` |
| `wallet_passes` | `wallet-passes`, `loyalty-wallet-passes` |
| `campaigns` | `campaigns` |
| `brands` | `brands` |
| `catalogs` | `catalogs`, `temp-menu` |
| `platforms` | `platforms` |
| `messaging` | `communication`, `push`, `inapp`, `news-feed`, `deeplinks` |
| `reviews` | `reviews`, `review-forms` |
| `tickets` | `tickets`, `ticket-pipelines`, `ticket-closure-forms` |

For example, `POST /behavior/events` needs `events:write` and
`GET /customers/{id}` needs `customers:read`. A request without the needed
scope gets `403` with `Insufficient scope. Required: <scope>`.

***

## OAuth apps (front ends)

An OAuth app tells Masivo to trust the ID tokens of your identity provider.
After the user signs in, your front end trades the provider's ID token for a
Masivo token that only reaches that customer's own data
([RFC 8693](https://datatracker.ietf.org/doc/html/rfc8693) token exchange):

```bash theme={null}
curl -X POST "https://app.masivo.ai/api/storefront/v1/oauth/exchange" \
  -d subject_token_type=urn:ietf:params:oauth:token-type:id_token \
  -d subject_token=$ID_TOKEN
```

```json theme={null}
{
  "access_token": "eyJ...",
  "token_type": "Bearer",
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "expires_in": 3600
}
```

See [Identity providers](https://docs.masivo.ai/api-reference/guides/oauth-identity-providers.md) for
the values of Firebase, Google, Auth0, Supabase, Cognito and Microsoft.

### App fields

| Field | Rules |
| - | - |
| Issuer | Public `https` URL. Must equal the token's `iss` |
| Audience | Must be one of the token's `aud` values |
| Alias | Optional. 2–63 lowercase letters, digits or hyphens. Required when a provider is shared |
| JWKS URL | Optional public `https` URL. Empty = discovered from `<issuer>/.well-known/openid-configuration` |
| Customer ID claim | Name of the claim whose value is the customer ID. Defaults to `sub` |

### How the exchange works

1. Masivo reads `iss` and `aud` and finds the active OAuth apps with that
   issuer and audience.
2. With several matches it uses the one whose alias equals the `audience`
   parameter.
3. It verifies the signature with the provider keys, plus issuer, audience
   and expiry.
4. It reads the **Customer ID claim** and signs a token with `scope: CUSTOMER`,
   `customer_id`, `oauth_app_id` and `env`.

### What a customer token can do

On `/customers/{id}`, only when `{id}` is its own `customer_id`:

| Path under `/customers/{own id}` | Methods |
| - | - |
| (profile) | `GET`, `PATCH` |
| `/consent` | `GET`, `PATCH` |
| `/devices` | `PATCH`, `DELETE` |
| `/devices/register`, `/devices/heartbeat` | `POST` |
| `/punch-cards`, `/transactions`, `/rewards/gift-cards`, `/score-streaks` | `GET` |
| `/inapp-messages`, `/news-feed`, `/wallet-passes` | `GET` |
| `/inapp-messages/*/events`, `/inapp-messages/*/actions`, `/news-feed/*/events` | `POST` |
| `/loyalty-wallet-passes` | `GET`, `POST` |
| `/referrals/request-code` | `POST` |

Outside `/customers` it reaches the same paths as a CLIENT API key:
`behavior/events`, `behavior/events/bulk`, `inapp/capabilities`,
`news-feed/capabilities`, `deeplinks/resolve`, `reviews` and `review-forms/*`.

* The profile `PATCH` only accepts `name`, `dob`, `gender`, `document_type`,
  `document_number`, `country` and `phone`.
* Every event must carry the token's `customer_id`, in bulk too.
* `PURCHASE` and other trusted writes still need an OAuth client.

### Customer ID claim

Masivo does not look customers up by email or any other field. It reads
**one claim of the ID token and uses its value, as is, as the Masivo
customer ID**. The **Customer ID claim** field holds the **name** of that
claim, never a value.

| Write in the field | Not |
| - | - |
| `sub` | `110101929639848822382` |
| `user_id` | `jane@example.com` |

**Why `sub` is the default.** OpenID Connect defines `sub` as the stable,
unique identifier of the user at the provider. Every provider sends it and it
never changes when the user edits their profile.

**Don't use `email`.** Masivo customer IDs only allow letters, digits, `_` and
`-` (`^[a-zA-Z0-9_-]+$`), so an email never matches a customer. Emails also
change, and some providers accept unverified ones.

**What this means for your customers.** Each customer must exist in Masivo
with the provider's user ID as its ID. Create it from your backend (OAuth
client with `customers:write`) when the user registers:

```bash theme={null}
curl -X POST "https://app.masivo.ai/api/storefront/v1/customers" \
  -H "Authorization: Bearer $SERVER_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"id":"<provider-user-id>","name":"Jane","email":"jane@example.com"}'
```

If your customers already have other Masivo IDs (for example `cust_123`), add
a custom claim with that ID in your provider and set **Customer ID claim** to
its name, for example `masivo_customer_id`.

### Sharing a provider between accounts

A brand with one account per country often has a single Firebase project for
all its apps. Each account can trust that provider with its own OAuth app:

1. Only a user who belongs to **every** account that already trusts the
   issuer and audience can add it to another account. Otherwise the dashboard
   answers *This issuer and audience belong to another account.*
2. Give each OAuth app a different **Alias** (for example `brand-ec` and
   `brand-pe`).
3. Each front end sends its alias as `audience` in the exchange. Without it
   the exchange fails with `invalid_request`; Masivo never guesses the account.

[Organizations](https://docs.masivo.ai/guides/settings/organizations.md) don't take part in this:
each OAuth app belongs to one account.

***

## Errors

Both endpoints answer standard OAuth errors (`{ "error", "error_description" }`):

| Error | Status | Meaning |
| - | - | - |
| `invalid_request` | 400 | Missing fields, or the provider is shared and no alias was sent |
| `invalid_client` | 401 | Wrong client ID or secret, or the client is disabled |
| `invalid_scope` | 400 | A requested scope is not granted to the client |
| `invalid_grant` | 400 | The ID token is invalid, expired or untrusted, its keys can't be fetched, or it lacks the customer ID claim |
| `invalid_target` | 400 | The `audience` alias doesn't match an OAuth app |
| `unauthorized_client` | 403 | The account is not active |
| `unsupported_grant_type` | 400 | Only `client_credentials` is supported at `/oauth/token` |

Tokens are valid until they expire (1 hour). Disabling a client or app, or
rotating a secret, stops new tokens but doesn't revoke tokens already issued.

> [!NOTE]
>
> The customer token exists only after the user signs in. Calls made before
> that (resolving deferred deep links, registering an install on first launch)
> still need a CLIENT API key.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.