# Tokens API

Whenever a user accesses an Embeddable dashboard in your app, you should fetch a **security token** from our **Tokens API**. This token applies [row-level security](https://docs.embeddable.com/data-modeling/row-level-security/introduction) (and more) to their dashboard session.

> Using **Cube Cloud** or another external Cube deployment? Pass the `dataProvider` field (see the [comprehensive example](#comprehensive-example) below) — set up in [Bring your own Cube](https://docs.embeddable.com/data/bring-your-own-cube).

API collection: https://github.com/embeddable-hq/embeddable-public-apis

## Example: Fetch a Token

**Important**: Always call this **server-side**—never from client code, to avoid exposing your API key.

```ts
//
// for security reasons: always call this from your server-side - never from client code
//
fetch('https://api.<region>.embeddable.com/api/v1/security-token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': `Bearer ${apiKey}` // Keep your API key secure
  },
  body: JSON.stringify({
    embeddableId: '<dashboard-id>', // the id of the dashboard you want to load
    savedVersion: 'production', // or 'development' or 'staging', etc.
    expiryInSeconds: 60 * 60 * 24 * 7, // token expiry (e.g. 1 week)
    securityContext: { // any context (key-value pairs) you want to provide to the SQL data models
      userId: '9sZSJ9LHsiYXR0cmlidXRlIjoiZ2VvaXBf',
      regions: ['us-east', 'eu-west']
    },
    user: 'example@domain.com', // unique key (e.g. email or user_id) representing current user
    //
    // for more fields, see "Comprehensive example" below
    //
  })
})
.then((resp) => resp.json())
.then(({ token }) => {
  // 'token' is the security token your `<em-beddable token={token}.../>` web component needs
  console.log(token);
});
```

**Response**:
```json
{
  "token": "eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiI1OGE1NGYwMi0xNTJmLTQxMjgtYWZmZC0zMDhhYzI0NTE5ZGQiLCJpYXQiOjE3MDczMTU1NDUsImV4cCI6MTcwNzkyMDM0NX0.lnovN0xTMCjLcxGTIfzFD9cYRIAZjn6S7sew-ih11lM"
}
```

You would then use this `token` in your `<em-beddable>` element, as explained in [Embedding an Embeddable](https://docs.embeddable.com/deployment/embedding#how-it-works).

Learn more about applying **row-level security** [here](https://docs.embeddable.com/data-modeling/row-level-security/introduction).

Learn more about specifying `region` [here](https://docs.embeddable.com/deployment/deployment-regions).

## Comprehensive example

```ts
//
// for security reasons: always call this from your server-side - never from client code
//
fetch('https://api.<region>.embeddable.com/api/v1/security-token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': `Bearer ${apiKey}` // Keep your API key secure
  },
  body: JSON.stringify({
    //
    // required fields
    //
    embeddableId: '<dashboard-id>', //the id of the dashboard you want to load
    savedVersion: 'production', // or 'development' or 'staging', etc.
    expiryInSeconds: 60 * 60 * 24 * 7, // token expiry (e.g. 1 week)
    securityContext: { // any context (key-value pairs) you want to provide to the SQL data models
      userId: '9sZSJ9LHsiYXR0cmlidXRlIjoiZ2VvaXBf',
      regions: ['us-east', 'eu-west']
    },
    user: 'example@domain.com', // unique key (e.g. email or user_id) representing current user

    //
    // optional fields
    //
    customCanvasState: 'user-1-dashboard-7', // optional key used by "custom canvas" functionality to store end-user custom dashboard state
    customCanvasReadOnly: false, // turns off custom dashboard editing for your end-user (if "custom canvas" is enabled)
    export: false, // set to true ONLY for tokens used with the Dashboard Export API (see "Exporting dashboards" below)

    //
    // managed Cube (default)
    //
    filters: [{ // optional, simplest way to enforce row-level security globally
      member: 'customers.id',
      operator: 'equals',
      values: ['<customer-id>'],
    }],
    environment: 'default', // optional, tells Embeddable which set of databases to retrieve data from
    roles: ['manager'], // optional, tells Embeddable which access-policy roles to grant this user e.g. manager, admin etc.
    
    //
    // external Cube (e.g. Cube Cloud)
    //
    dataProvider: 'my-cube-deployment' // tells Embeddable which external Cube deployment to connect to
  })
})
.then((resp) => resp.json())
.then(({ token }) => {
  // 'token' is the security token your `<em-beddable token={token}.../>` web component needs
  console.log(token);
});
```

### Parameters

- **`embeddableId`** is the id of the Embeddable dashboard that you want to load.  Click 'save version' or 'publish' on your Embeddable dashboard to get the id.
- **`savedVersion`** this is the [saved version](https://docs.embeddable.com/deployment/versions) of the Embeddable dashboard that you want to load.  Available options include: 
  - 'production' (default) - the saved version published with the 'Production' tag.  This will be used by default if `savedVersion` is omitted.
  - 'staging' - the saved version published with the 'Staging' tag
  - 'development' - the saved version published with the 'Development' tag
  - 'v3' (or v11, etc.) - the saved version with the given version number.
- **`securityContext`** is an arbitrary JSON object included in your token request. Embeddable passes it to your **data models**, allowing you to enforce row-level (as well as table and schema-level) security (learn more [here](https://docs.embeddable.com/data-modeling/row-level-security/sql-based-security)). For example:

    ```sql
    name: Orders
    sql: >
        SELECT user_id, created_at
        FROM public.orders
        WHERE user_id = '{ COMPILE_CONTEXT.securityContext.userId }'
        
        {% if COMPILE_CONTEXT.securityContext.regions %}
            AND region IN {{ list(COMPILE_CONTEXT.securityContext.regions) }}
        {% endif %}
    ```
    
- **`roles`** optional field, only needed if you’re using [Access Policies](https://docs.embeddable.com/data-modeling/row-level-security/access-policies). Grants the user one or more access policy roles. The roles you include determine which [cube models](https://docs.embeddable.com/data-modeling/writing-data-models/defining-models), [views](https://docs.embeddable.com/data-modeling/writing-data-models/views), and rows the user can see across [dashboards](https://docs.embeddable.com/dashboards/introduction) and [data playground](https://docs.embeddable.com/data-modeling/testing-models). If you don’t pass any roles, Embeddable assigns the "default" role. Refer to [Access Policies](https://docs.embeddable.com/data-modeling/row-level-security/access-policies) for details on how roles drive visibility.

- **`filters`** optional array of filter objects, each with `member`, `operator`, and `values`. When provided, Embeddable automatically applies these filters to every query in the dashboard session. Any query that cannot apply the filter (for example, because there is no join path to the filtered model) will fail with an error. This is the simplest way to ensure row-level security is applied globally across all your data models. See [Security Filters](https://docs.embeddable.com/data-modeling/row-level-security/security-filters#security-filters) for details.

- **`user`** is a unique identifier for the current user. It enables Embeddable to store (anonymous) session metadata against each user automatically so that customisations / preferences applied by the user to their Embeddable dashboard will automatically be remembered for their next session.

- **`environment`** optional field, only needed if you're using [Environments](https://docs.embeddable.com/data/environments). Specifies which [environment](https://docs.embeddable.com/data/environments) to retrieve data from. If omitted, the "default" environment is used. [Environments](https://docs.embeddable.com/data/environments) let you achieve things like:
    - **single-tenancy** (AKA **database-level security**), where each of your customers has their data in a different database.
    - different database connections for **prod**, **qa**, **testing**, etc. 
    - **micro service architecture**, where your data is spread across multiple databases.

- **`dataProvider`** is required if (and only if) you are [bringing your own Cube deployment](https://docs.embeddable.com/data/bring-your-own-cube). Specifies which external Cube deployment Embeddable should connect to. This must match a data provider you have created using the [**Data Provider API**](https://docs.embeddable.com/data/cube-cloud#data-provider-api) (for example, `my-cube-deployment`). 

- **`expiryInSeconds`** is the number of seconds until the token expires. The default is 7 days (60 * 60 * 24 * 7). Tokens are standard JWTs, and contain `iat` and `exp` when decoded. On your front-end, you can keep track of the token expiry and refresh it as needed with a call to the Embeddable Tokens API, without needing to reload the page.

- **`customCanvasState`** is only needed if you are using [Custom Canvas](https://docs.embeddable.com/self-serve/custom-canvas/introduction). Embeddable stores a custom canvas layout (per `embeddableId`) for each unique value of `customCanvasState` that you pass to the Tokens API.  Learn how to use this field [here](./custom-canvas).  

- **`customCanvasReadOnly`** is also only needed if you are using [Custom Canvas](https://docs.embeddable.com/self-serve/custom-canvas/introduction).  If `true` the end-user will only be able to view (and interact with) the dashboard, but not edit it (add/remove/move/resize charts). It defaults to `false`.  Learn more [here](./custom-canvas).

- **`export`** is only needed if you are using the [Dashboard Export API](https://docs.embeddable.com/deployment/dashboard-export). It defaults to `false`. Set it to `true` **only** for a token you pass to that API. An export token has a different lifecycle to a normal token, so the two are not interchangeable — see [Exporting dashboards](#exporting-dashboards) below.

## Exporting dashboards

To export a dashboard with the [Dashboard Export API](https://docs.embeddable.com/deployment/dashboard-export), request a token with **`export: true`** and pass it to that API. Everything else about the token works as usual — RLS, `filters`, `roles`, `securityContext`, `variables` and `savedVersion` all apply to the export exactly as they would to an embedded dashboard.

Export tokens and normal embedding tokens are **not interchangeable**:

- A token **without** `export: true` is **rejected** by the Dashboard Export API.
- A token **with** `export: true` is **rejected** if you try to use it to embed a dashboard normally (in your `<em-beddable>` element).

> Create a fresh token with `export: true` for each export, and use your normal tokens for embedding. Learn more in the [Dashboard Export API](https://docs.embeddable.com/deployment/dashboard-export) guide.

## Testing Security Contexts

If you’re relying on `securityContext` in your data models, you’ll want to **test** them in Embeddable’s no-code builder. You can easily do this by creating [presets](https://docs.embeddable.com/data-modeling/row-level-security/sql-based-security#testing-your-sql-based-security).

---
Source: https://docs.embeddable.com/deployment/tokens-api
