# Authentication

The **vrtx closed-loop APIs** use a secure **API Key + Secret Key** authentication model.
Your backend exchanges these credentials for a **Bearer Access Token**, which is then used to authenticate all API requests.

---

## <Icon icon="material-outline-density_small"/> Overview

All vrtx API calls follow this flow:

1. Your backend sends `client_id` and `client_secret` to the [Token Endpoint](https://docs.vrtx.sa/23284064e0.md)
2. vrtx returns a **Bearer token**
3. The Bearer token is used in every subsequent request
4. When it expires, generate a new one using the same credentials


---

## <Icon icon="material-outline-not_started"/> Before You Start

<Steps>
  <Step title="Create an account on vrtx">
    A sandbox business is created for you with a developer access. [Get your account!](https://www.vrtx.sa/contact)
  </Step>

  <Step title="Retrieve Your Credentials">
    From the vrtx dashboard, navigate to **Developers → API Keys** to obtain:
    - `X-API-KEY`
    - `client_secret`
  </Step>

  <Step title="Generate a Bearer Token">
    Call the `/auth/token` endpoint using your API credentials.
  </Step>

  <Step title="Store Token Securely">
    Save the access token on your server environment.
  </Step>

  <Step title="Authenticate All Requests">
    Send the token using:
    `Authorization: Bearer access_token`
  </Step>
      
</Steps>

---

## <Icon icon="material-outline-swap_vertical_circle"/> Authentication Model

<CardGroup cols={3}>
  <Card title="🔐 Secure Access">
    The API Key and Secret Key authenticate your backend securely.
  </Card>
  <Card title="⏱ Short-Lived Tokens">
    Bearer tokens have a limited lifespan for improved security.
  </Card>
  <Card title="🔄 Easy Rotation">
    Rotate keys anytime from the vrtx developer dashboard.
  </Card>
</CardGroup>

---

## <Icon icon="material-outline-switch_access_shortcut"/> Token Endpoint

vrtx uses a single endpoint to generate access tokens: [Token](https://docs.vrtx.sa/23284064e0.md)

| Item               | Value                                               |
| ------------------ | --------------------------------------------------- |
| **URL**            | `POST /v1/auth/token`                               |
| **Authentication** | `client_id` + `client_secret`                        |
| **Token Type**     | `Bearer`                                            |
| **Expires In**     | Defined by system configuration (e.g. 3600 seconds) |

### Request

```js
grant_type: client_credentials
client_id: <your_client_id>
client_secret: <your_client_secret>
scope: closedloop.read closedloop.write
content-Type: application/json
```

### Response

```json
{
  "access_token": "vrtx_at_93bf2d181ab",
  "expires_in": 3600,
  "token_type": "Bearer"
}
```

## <Icon icon="material-outline-verified_user"/> Using the Token

Once generated, include the token in every API request:

```js
Authorization: Bearer <access_token>
```

### Example: Get Wallets

```js
curl -X GET https://api.vrtx.sa/v1/wallets \
  -H "Authorization: Bearer <access_token>"
```

---

## <Icon icon="material-outline-workspaces"/> Understanding Scopes

Scopes determine *what your token is allowed to do* inside the vrtx closed-loop ecosystem.

Scopes are always formatted as:

```js
closedloop.read
closedloop.write
```

:::info[Read/Write Scope]
If you include both scoping values, separate them with a space:

```js
closedloop.read closedloop.write
```
:::
You can pass one or both scopes in the `scope` parameter.

| Function             | Read | Write |
| -------------------- | ---- | ----- |
| Fetch Data           | ✔️    | ✔️     |
| Create / Update      | ❌   | ✔️     |
| Financial Operations | ❌   | ✔️     |

:::caution[Be careful]
Use the scope that fits your needs. In production, use read and write to achieve:
* Full visibility
* Execute operations
:::

### Best Practice for Choosing Scopes

| Use Case                                             | Recommended Scope                            |
| ---------------------------------------------------- | -------------------------------------------- |
| Dashboards / Reporting / Analytics                   | `closedloop.read`                            |
| Operational systems (wallet funding, payouts, cards) | `closedloop.write`                           |
| Full backend integration in production               | `closedloop.read closedloop.write`           |
| Third-party integrations                             | Least privilege (read only unless necessary) |




