# Authentication

> How to obtain access tokens for the B2B2X public API, including service-account credentials, human sign-in via token exchange, and the sandbox environment.

Source: https://business-api-docs.youhodler.com/integrations/authentication

The B2B2X public API is bearer-token authenticated. Tokens are issued
by `POST /oauth/token` and presented in the `Authorization: Bearer`
header on every subsequent call. There are two integration patterns,
matching the two
[actor types](/concepts/identity#actor-types) the platform
recognises.

> **Practical rule:** Use a service principal token for routine machine-to-machine traffic. Use a human token obtained through token exchange when an action requires an eligible human decision.

## Base URL

**Sandbox**

```
https://b2b2x-sandbox.youhodler.com/api
```

**Production**

```
https://b2b.youhodler.com/api
```

## Sandbox

Sandbox tenants and credentials are issued through the
[business-api landing page](https://business-api.youhodler.com/).
Until a tenant is provisioned, `POST /oauth/token` calls against the
sandbox return `400 invalid_request` with the message `invalid credentials`
— that is expected, not a contract issue.

See [Sandbox simulations](/guides/sandbox-testing) for the currency-to-rail
support table and an ARS deposit example.

## Service Principal Credentials

Each service account has a `client_id` and a `client_secret` that are
shown once at creation. The `client_id` is the service account's `id`, a
UUID. Store the pair as you would any production credential.

Exchange them for an access token with the OAuth 2.0 client-credentials
grant:

**Request URL — POST**
```http
POST /oauth/token
```
**Request Body — application/json**
```json
{
  "grant_type": "client_credentials",
  "client_id": "b8e2f1a0-4c3d-4e5f-9a1b-2c3d4e5f6a7b",
  "client_secret": "<shown once at creation>"
}
```
**Response — application/json**
```json
{
  "access_token": "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJiOGUyZjFhMCJ9.sig",
  "expires_in": 3600,
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "token_type": "Bearer"
}
```

The resulting token's effective actor is the service principal itself.
Authorization is checked against the role assignments granted to that
service principal — see [Access](/concepts/access).

### Refused Credentials

`POST /oauth/token` refuses a bad client credential with `400 invalid_request`,
`data.reason` `missing_required_field` and the message `invalid credentials`.
An unknown or non-UUID `client_id`, a wrong or expired secret and a revoked
service account all receive this same response, so it does not reveal which
client ids exist. Omitting `client_id` or `client_secret` is also a
`400 invalid_request`, and a missing or unsupported `grant_type` returns
`400 unsupported_grant_type`.

The token endpoint returns `401 unauthorized` only with `data.reason`
`assertion_predates_suspension`: the subject token or client assertion was
issued before a suspension of the human or the identity provider. Obtain a
fresh assertion from the identity provider before retrying.

## Acting On Behalf Of A Human

For actions that require an identifiable human actor of record
(approvals, sensitive transitions, governance changes) the integration
exchanges a federated human's identity assertion for a platform token
that *acts on behalf of* the human.

This uses RFC 8693 token exchange:

**Request URL — POST**
```http
POST /oauth/token
```
**Request Body — application/json**
```json
{
  "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
  "client_id": "b8e2f1a0-4c3d-4e5f-9a1b-2c3d4e5f6a7b",
  "client_secret": "<...>",
  "subject_token": "<id_token or JWT issued by a configured IdP>",
  "subject_token_type": "urn:ietf:params:oauth:token-type:id_token"
}
```
**Response — application/json**
```json
{
  "access_token": "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJiOGUyZjFhMCJ9.sig",
  "expires_in": 3600,
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "token_type": "Bearer"
}
```

The resulting token's `subject` is the human; the integration's service
client credentials select the workspace; the resulting public-edge principal
and subject are the human. Authorization uses that human's role assignments.
The public API rejects delegated `actor_token` exchange and `x-on-behalf-of`
headers; it does not preserve a separate service transport actor. See
[Identity → Acting On Behalf Of A Human](/concepts/identity#acting-on-behalf-of-a-human)
for the canonical model.

The `password` grant is intentionally not supported. Human authentication
always goes through an external identity provider.

## Using The Token

Send the access token on every API call:

```
Authorization: Bearer <access_token>
Content-Type: application/json
Idempotency-Key: <unique key>    # accepted on POSTs; required where documented
If-Match: <current ETag>         # only where the endpoint requires it
```

See [Idempotency And Concurrency](/concepts/idempotency-and-concurrency)
for the headers, and [Errors](/concepts/errors) for what to expect
when something goes wrong.

## Token Expiry And Refresh

Tokens expire after `expires_in` seconds. Re-issue a fresh token by
calling `POST /oauth/token` again with the same credentials — the
client-credentials flow does not return a refresh token. Token responses bypass
the durable idempotency cache, so each request issues a fresh token.
Service-account creation differs: a retry with the same `Idempotency-Key`
returns the created account without `client_secret`. See
[Idempotency And Concurrency](/concepts/idempotency-and-concurrency#idempotency).

Partner token exchanges authenticated with `client_id` and `client_secret`
also return an access token only. A fresh exchange requires a fresh
`subject_token` from the IdP.

Direct console sign-in through a registered IdP, without client credentials,
returns `refresh_token` and `session_expires_in` together. The session has a
one-hour idle limit and a twelve-hour absolute limit by default. Renew with
`grant_type=refresh_token`; renewal rotates the refresh token and never resets
the absolute limit or makes the authentication evidence more recent.

Persist a cryptographically random `next_refresh_token` with the current token
before renewal. If the response is lost, an ordinary refresh with the same
pair can recover the committed successor without extending its deadlines.
Retrying a spent token without its active successor revokes the session.
See [Issue access token](/api/reference/auth/oauth-token) for the exact
recovery and reauthentication-promotion contract.

On sign-out, call [Revoke a refresh token](/api/reference/auth/oauth-revoke).
Revocation ends the refresh session; already-issued access tokens retain their
normal expiry. Approval decisions and action-token issuance require authentication
no older than fifteen minutes and can request reauthentication before proceeding.
