# Authentication

The Nimbuz API uses **JWT bearer tokens**. Log in with the same credentials you use for the portal, then send the token on every request:

```http
Authorization: Bearer <token>
```

<Stepper>

1. **Log in**

   Call [`POST /user/v1/user/login`](/reference/authentication). Individual accounts use `orgName: "nimbuz"`. Business accounts use their organization name, which is the sub-domain they sign in on.

   <CodeTabs>

   ```shell title="cURL"
   curl -s https://api.nimbuz.tech/user/v1/user/login \
     -H 'Content-Type: application/json' \
     -d '{"username":"jane@example.com","password":"your-password","orgName":"nimbuz"}'
   ```

   ```js title="JavaScript"
   const res = await fetch("https://api.nimbuz.tech/user/v1/user/login", {
     method: "POST",
     headers: { "Content-Type": "application/json" },
     body: JSON.stringify({
       username: "jane@example.com",
       password: process.env.NIMBUZ_PASSWORD,
       orgName: "nimbuz",
     }),
   });
   const { data } = await res.json();
   const token = data.token;
   ```

   ```python title="Python"
   import os, requests

   r = requests.post("https://api.nimbuz.tech/user/v1/user/login", json={
       "username": "jane@example.com",
       "password": os.environ["NIMBUZ_PASSWORD"],
       "orgName": "nimbuz",
   })
   r.raise_for_status()
   token = r.json()["data"]["token"]
   ```

   </CodeTabs>

   ```json title="Response"
   {
     "status": "success",
     "message": "Token fetched successfully",
     "data": { "token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..." }
   }
   ```

2. **Send the token**

   <CodeTabs>

   ```shell title="cURL"
   export NIMBUZ_TOKEN=eyJhbGciOi...
   curl -s https://api.nimbuz.tech/project/v1/projects \
     -H "Authorization: Bearer $NIMBUZ_TOKEN"
   ```

   ```js title="JavaScript"
   const projects = await fetch("https://api.nimbuz.tech/project/v1/projects", {
     headers: { Authorization: `Bearer ${token}` },
   }).then((r) => r.json());
   ```

   ```python title="Python"
   s = requests.Session()
   s.headers["Authorization"] = f"Bearer {token}"
   projects = s.get("https://api.nimbuz.tech/project/v1/projects").json()
   ```

   </CodeTabs>

3. **Re-login when it expires**

   There is **no refresh token**. When any call returns `401`, log in again and retry the request.

</Stepper>

:::tip{title="Try it in the playground"}
Every endpoint in the [API Reference](/reference) has a **Test** button. Paste your token into the Bearer auth field once, and the playground sends it on every request.
:::

## Two-factor authentication

If 2FA is enabled, login returns a challenge instead of a token:

```json
{
  "status": "success",
  "data": {
    "mfaRequired": true,
    "challengeId": "6f1c1e0a-3b7e-4c7f-9a51-2d6f0f6d2c11",
    "methods": ["totp", "email"],
    "expiresIn": 300
  }
}
```

Before `expiresIn` seconds have passed, send the 6-digit code from your authenticator app:

```shell
curl -s https://api.nimbuz.tech/user/v1/user/login/2fa/verify \
  -H 'Content-Type: application/json' \
  -d '{"challengeId":"6f1c1e0a-3b7e-4c7f-9a51-2d6f0f6d2c11","code":"492817"}'
```

The response contains `data.token`, the same as a normal login.

:::note{title="CI pipelines"}
Use a dedicated Nimbuz user without 2FA that is a member of only the projects the pipeline deploys to. Keep its credentials in your CI secret store.
:::

## Your account ID

Billing endpoints need an `account_id`. It's stored in the token, in the `properties.nimbuzAccountId` claim:

<CodeTabs>

```shell title="cURL"
echo $NIMBUZ_TOKEN | cut -d. -f2 | base64 -d 2>/dev/null | jq -r '.properties.nimbuzAccountId'
```

```js title="JavaScript"
const claims = JSON.parse(atob(token.split(".")[1]));
const accountId = claims.properties.nimbuzAccountId;
```

```python title="Python"
import base64, json
payload = token.split(".")[1] + "=="
account_id = json.loads(base64.urlsafe_b64decode(payload))["properties"]["nimbuzAccountId"]
```

</CodeTabs>

## Login errors

| Status | Code                                     | What to do                                  |
| ------ | ---------------------------------------- | ------------------------------------------- |
| `400`  | `EMAIL_NOT_VERIFIED`                     | Verify your e-mail from the sign-up message. |
| `400`  | `INVALID_ORGANIZATION`                   | Check `orgName`.                            |
| `403`  | `ACCOUNT_SUSPENDED` / `ACCOUNT_INACTIVE` | Contact support.                            |
| `429`  | `MFA_TOO_MANY_ATTEMPTS`                  | Start a new login.                          |
