> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authenticate serverless inference with workload identity federation

> Exchange your application's existing identity token for a federated access token and call Serverless Inference.

Workload identity federation lets your application use its existing OpenID Connect (OIDC) identity to call Serverless Inference without storing a long-lived Forge API key. Your application exchanges a signed JSON Web Token (JWT) for an expiring federated access token and sends that access token with its inference requests.

Forge admins configure trust for the application's identity, and developers update the application to exchange its JWT.

To authenticate with an API key, see [Authentication](https://docs.coreweave.com/products/inference/serverless/api-reference.md#authentication).

## Configure trust in Forge

Organization and team admins complete this one-time setup in multi-tenant Forge. To send inference requests, your organization needs access to Serverless Inference. To create a federated team service account, your organization needs an Enterprise plan.

### Register the JWT issuer

As a Forge organization admin, register the application's existing issuer.

1. In the upper-right corner, click the account selector.
2. Click **Settings**.
3. In the sidebar, click **Authentication**.
4. Under **JWT Issuers**, click **Add JWT Issuer**.
5. For **Issuer URL**, enter the issuer URL. The URL must exactly match the `iss` claim in the application's JWT.
6. Click **Add issuer**.

Forge reads the issuer's publicly accessible OIDC discovery document at `ISSUER_URL/.well-known/openid-configuration`, where `ISSUER_URL` is the issuer URL you entered. It uses the public keys from the discovery process to verify JWT signatures.

### Create a federated service account

As a team admin, create the service account in the team that owns your inference project.

1. In Forge, open that team's **Service Accounts** tab.
2. Click **New Team Service Account**.
3. Enter a **Name**.
4. For **Authentication Method**, select **Federated identity**.
5. For **JWT Issuer**, select the issuer registered by your organization admin.
6. For **Subject**, enter the exact `sub` claim from a JWT issued to the application.
7. Click **Create service account**.

Forge uses the registered issuer and subject to identify the service account during token exchange.

## Authenticate from your application

Use Bash with `curl` and `jq` to exchange the JWT. Keep the same shell open for the chat completion example.

### Exchange a workload token

Get a signed, unexpired JWT from the registered issuer. Its `sub` claim must match the configured subject. Its single `aud` value must match the exact name of the Forge organization that contains your project. Set `OIDC_TOKEN_FILE` to the absolute path of the file containing the JWT.

```bash theme={"system"}
set +x

exchange_workload_token() {
  local response token ttl
  unset INFERENCE_BEARER_TOKEN INFERENCE_EXPIRES_IN

  response="$(
    curl --silent --show-error --fail --max-time 15 \
      --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer' \
      --data-urlencode "assertion@${OIDC_TOKEN_FILE:?Set OIDC_TOKEN_FILE}" \
      https://api.forge.coreweave.com/oidc/token
  )" || return 1
  token="$(jq -er '.access_token | strings | select(length > 0)' <<< "$response")" || return 1
  ttl="$(jq -er '.expires_in | numbers | select(. > 0)' <<< "$response")" || return 1

  INFERENCE_BEARER_TOKEN="$token"
  INFERENCE_EXPIRES_IN="$ttl"
}

exchange_workload_token
```

On success, `exchange_workload_token()` saves the access token in `INFERENCE_BEARER_TOKEN` and its lifetime in seconds in `INFERENCE_EXPIRES_IN`. If the exchange fails, resolve the error before sending an inference request.

### Send a chat completion request

For this example, choose an existing project with **Team** visibility that the service account can write to. Set `INFERENCE_PROJECT` to its `team/project` identifier by replacing `TEAM_NAME` and `PROJECT_NAME` with the team and project names.

```bash theme={"system"}
export INFERENCE_PROJECT='TEAM_NAME/PROJECT_NAME'
```

Both examples send the identifier in the `OpenAI-Project` header and use the exchanged access token as the bearer credential.

> [tab] Bash
>
> ```bash theme={"system"}
> set -o pipefail
>
> printf 'Authorization: Bearer %s\n' "${INFERENCE_BEARER_TOKEN:?Set INFERENCE_BEARER_TOKEN}" |
>   curl --silent --show-error --fail --max-time 60 \
>     --header @- \
>     --header "OpenAI-Project: ${INFERENCE_PROJECT:?Set INFERENCE_PROJECT}" \
>     --header 'Content-Type: application/json' \
>     --data '{
>       "model": "meta-llama/Llama-3.1-8B-Instruct",
>       "messages": [{"role": "user", "content": "Reply with hello."}],
>       "max_completion_tokens": 32
>     }' \
>     https://api.inference.wandb.ai/v1/chat/completions |
>   jq -er '.choices[0].message.content'
> ```

> [tab] Python
>
> In the same Bash session, install the OpenAI Python package and export the token.
>
> ```bash theme={"system"}
> python -m pip install openai
> export INFERENCE_BEARER_TOKEN
> ```
>
> Run the Python example from that session.
>
> ```python theme={"system"}
> import os
> from openai import OpenAI
>
> client = OpenAI(
>     base_url="https://api.inference.wandb.ai/v1",
>     api_key=os.environ["INFERENCE_BEARER_TOKEN"],
>     project=os.environ["INFERENCE_PROJECT"],
> )
>
> completion = client.chat.completions.create(
>     model="meta-llama/Llama-3.1-8B-Instruct",
>     messages=[{"role": "user", "content": "Reply with hello."}],
>     max_completion_tokens=32,
> )
> print(completion.choices[0].message.content)
> ```

Both examples print the assistant's reply. For the response format, see [Chat completions](https://docs.coreweave.com/products/inference/serverless/api-reference/chat-completions.md).

### Renew the access token

Reuse the access token across requests. Schedule another exchange before `INFERENCE_EXPIRES_IN` seconds elapse. Under the default **Token renewal** setting, supply a fresh JWT for each exchange. For the Python example, export the new token and create a new client. Keep both tokens out of logs.

### Troubleshoot authentication

If the exchange fails, check that Forge can reach the issuer's discovery document and public keys. Verify these JWT values:

* Expiration
* Issuer
* Subject
* Audience

Under the default issuer policy, a `jti_known` error means that Forge already exchanged the JWT. Get a fresh JWT and retry.

If the exchange succeeds and the inference request fails authorization, check that the request uses the exchanged access token and the intended `OpenAI-Project` value. Confirm that the service account can write to that project.
