> Source: https://developers.terrawallet.us/docs/authentication . Terra Wallet developer docs (early access).

# Authentication

Your server calls the Terra API with a **secret key**. Browser widgets use a **publishable key** that works only
on the origins you allow. Both come from the dashboard, per project and per environment.

> **Preview.** The API is in early access and may change before launch.

## Key types

| Prefix | Type | Where it goes | What it can do |
|---|---|---|---|
| `tw_test_sk_` | Secret, test | Your server only | Test mode (testnets), limited to its scopes |
| `tw_live_sk_` | Secret, live | Your server only | Live mode (mainnets), limited to its scopes |
| `tw_test_pk_` | Publishable, test | Browser or app | Only checkout and connect scopes, only on your allowed origins |
| `tw_live_pk_` | Publishable, live | Browser or app | Only checkout and connect scopes, only on your allowed origins |

- A secret key is the prefix plus 43 random characters (32 bytes, base64url). Terra stores only a keyed hash
  of it and an 8-character display prefix, such as `tw_live_sk_AbC12xYz`. The full key is shown **once**, when you
  create or rotate it. Support cannot recover a lost key: rotate it.
- Never put a secret key in a browser, a mobile app, a URL or a git repository. Base64url keys contain `-` and
  `_`, so use the dashboard's copy button rather than double-clicking.

## Sending the key

Send secret keys only in the `Authorization` header:

```bash
curl https://terrawallet.us/api/v1/payment_intents \
  -H "Authorization: Bearer $TERRA_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-1042-checkout" \
  -d '{ ... }'
```

Keys are never accepted in a query string or a request body, which keeps them out of access logs and `Referer`
headers.

**The key decides the organization, project and environment.** A request body can never choose them, and a test
key can never read or change live objects (or the reverse).

## Test and live environments

Every organization has two environments:

- **test** runs on testnets only (see [Test mode](https://developers.terrawallet.us/docs/pay/test-mode.md));
- **live** runs on mainnets. Live actions need an organization Owner or Admin with an authenticator app enrolled,
  and live mode must be switched on for the platform. Otherwise live actions return `403 LIVE_DISABLED`.

## Scopes

- Scopes come from a fixed list. An unknown scope is a validation error, and a key with no scopes can do
  nothing.
- Every route declares the scope it needs. A key without it gets `403 INSUFFICIENT_SCOPE`.
- Examples of scopes: `projects:read`, `keys:read`, `audit:read`, and for later products scopes such as
  `payments:write` and `webhooks:write`. The API reference lists the scope each route needs once the OpenAPI
  document is published.
- Give each key only the scopes it needs. A read-only key for a reporting job cannot create payments.

## Publishable keys and allowed origins

- Publishable keys carry only checkout and connect scopes.
- They work only on the exact https origins you set for the project
  (`PUT /v1/projects/:id/origins`), for example `https://shop.example.com`. No wildcards and no paths.
- They are safe to ship in front-end code because they cannot create payments, read your data or touch your
  payout wallets.

## Rotation and revocation

- `POST /v1/keys/:id/rotate` creates a new key with the same project, environment, label and scopes. The old
  key keeps working for 24 hours so you can switch without downtime.
- `DELETE /v1/keys/:id` revokes a key at once.
- Creating, rotating or revoking a live key needs a fresh authenticator code.
- Every create, rotate and revoke is written to the audit log with the display prefix, never the key.

## Failed authentication

A malformed, unknown, expired or revoked key gets the same `401 INVALID_API_KEY` response after the same amount
of work, so the response never reveals whether a key exists.

## Dashboard sessions

The dashboard at `dashboard.terrawallet.us` signs you in with email and password, Google, GitHub or X, followed
by an authenticator code when you have one enrolled. Its API calls use a session cookie on the same origin, plus
a CSRF header on every state-changing request. Server integrations never use sessions: use a secret key.

- Sessions last 12 hours idle and 7 days at most, and rotate on privilege changes.
- Passwords are at least 10 characters and are hashed with argon2id.
- Repeated failures lock the account (10 failures in 15 minutes) with exponential backoff.

## Roles

Organization members have one of six roles: owner, admin, developer, finance, analyst and readonly. Owner, Admin
and Developer can manage keys.
