---
title: "Authentication"
---

Authenticate as a SaaSify user to get a bearer token, then send that token with every request.

:::note
Base URLs: `https://api.saasify.ai` (production). Test against
`https://api-demo.saasify.ai` (demo). See [Environments](/developers/environments).
:::

SaaSify uses the OAuth 2.0 resource-owner password grant. You exchange a SaaSify username and password for an opaque bearer token at `POST /api/token`, then send `Authorization: Bearer <token>` on each call. The token carries the user's tenant and [role](/get-started/roles), so there are no separate API keys or scopes to manage today.

:::tip[Use a dedicated service user]
Create a SaaSify user whose only job is API access, give it a least-privilege role that covers the operations you call, and authenticate as that user — not as a person's account. The token inherits the user's full role permissions, so keep the role tight.
:::

## Get a token

Send a form-encoded `POST` to `/api/token` with `grant_type=password` and the user's `username` and `password`. Add the `TenantCode` header if the user belongs to more than one tenant (see [Multi-tenant users](#multi-tenant-users)).

:::code-group
```bash title="cURL"
curl -X POST https://api-demo.saasify.ai/api/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "TenantCode: YOUR_TENANT_CODE" \
  --data-urlencode "grant_type=password" \
  --data-urlencode "username=service-user@example.com" \
  --data-urlencode "password=YOUR_PASSWORD"
```
```python title="Python"
import requests

resp = requests.post(
    "https://api-demo.saasify.ai/api/token",
    headers={"TenantCode": "YOUR_TENANT_CODE"},  # optional
    data={
        "grant_type": "password",
        "username": "service-user@example.com",
        "password": "YOUR_PASSWORD",
    },
)
resp.raise_for_status()
token = resp.json()["access_token"]
```
```javascript title="Node"
const body = new URLSearchParams({
  grant_type: "password",
  username: "service-user@example.com",
  password: "YOUR_PASSWORD",
});

const resp = await fetch("https://api-demo.saasify.ai/api/token", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "TenantCode": "YOUR_TENANT_CODE", // optional
  },
  body,
});
const { access_token: token } = await resp.json();
```
```csharp title="C#"
using var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post,
    "https://api-demo.saasify.ai/api/token");
request.Headers.Add("TenantCode", "YOUR_TENANT_CODE"); // optional
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
    ["grant_type"] = "password",
    ["username"]   = "service-user@example.com",
    ["password"]   = "YOUR_PASSWORD",
});

var resp = await client.SendAsync(request);
resp.EnsureSuccessStatusCode();
using var doc = System.Text.Json.JsonDocument.Parse(
    await resp.Content.ReadAsStringAsync());
var token = doc.RootElement.GetProperty("access_token").GetString();
```
:::

The response is a standard OAuth token payload:

```json
{
  "access_token": "a-long-opaque-token",
  "token_type": "bearer",
  "expires_in": 86400,
  "refresh_token": "another-opaque-token",
  "userName": "service-user@example.com",
  "isTenantUser": true,
  "onBehalfOf": null
}
```

The token is opaque — treat it as a string, do not try to decode it. It is valid for `expires_in` seconds (24 hours / 1,440 minutes in the current production configuration). Store it and reuse it until it expires rather than requesting a new token on every call.

:::warning
Do not send the `X-SaaSify-Client` header. That header switches the token endpoint into the web app's cookie mode, which moves the tokens into HttpOnly cookies and removes them from the response body. API callers should leave it out and read the token from the body.
:::

## Send the token

Put the token in an `Authorization: Bearer` header on every request:

:::code-group
```bash title="cURL"
curl https://api-demo.saasify.ai/api/subscriptions/list \
  -H "Authorization: Bearer $TOKEN"
```
```python title="Python"
headers = {"Authorization": f"Bearer {token}"}
r = requests.get("https://api-demo.saasify.ai/api/subscriptions/list", headers=headers)
```
```javascript title="Node"
const r = await fetch("https://api-demo.saasify.ai/api/subscriptions/list", {
  headers: { Authorization: `Bearer ${token}` },
});
```
```csharp title="C#"
client.DefaultRequestHeaders.Authorization =
    new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
```
:::

The tenant comes from the token, so an authenticated call usually needs only the bearer token — no extra tenant headers.

## Refresh a token

When a token expires, exchange the `refresh_token` for a new one instead of asking the user for their password again. Use the same `/api/token` endpoint with `grant_type=refresh_token`:

```bash
curl -X POST https://api-demo.saasify.ai/api/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=refresh_token" \
  --data-urlencode "refresh_token=YOUR_REFRESH_TOKEN"
```

The response has the same shape as the password-grant response, with a fresh `access_token`.

## Multi-tenant users {#multi-tenant-users}

If a user belongs to a single tenant, you can omit `TenantCode` — the tenant is resolved from the user. If the user belongs to more than one tenant, send the `TenantCode` header to choose which tenant the token (and later requests) act in. You can get a user's tenant code from a Publisher Admin.

## SSO-only tenants

SaaSify users who sign in through single sign-on (Okta, Microsoft Entra ID, Google and similar) cannot use the password grant, because they have no SaaSify password. If your tenant enforces SSO for people, you still need one **local service user with a password** for API access. Create it under **Settings → Users** as an **Add local user**, give it an API-appropriate role, and use its credentials with the password grant above.

## What does not exist yet

- No API keys and no client-credentials grant. Authentication is always as a user.
- No OAuth scopes. Access is governed by the user's role permissions.
- No per-ISV OAuth app registration and no request signing for your calls.

:::note
These are planned. When machine credentials ship, this page will cover them. Until then, the service-user pattern above is the supported approach.
:::

## Related

::::cards
:::card[Environments]{href="/developers/environments"}
Which base URL to use.
:::
:::card[Quickstart]{href="/developers/quickstart"}
Token to first call.
:::
:::card[Conventions]{href="/developers/conventions"}
The response envelope and error handling.
:::
:::card[Roles and permissions]{href="/get-started/roles"}
Pick a role for your service user.
:::
::::
