Skip to content
Application identity

Configure Vercel Workload Identity

Authenticate Vercel Functions to Keycard with the deployment's OIDC token.

A Vercel function gets a short-lived OIDC token for its project and environment. It sends the token to Keycard along with the Client ID of the Application credential it authenticates as. Keycard checks the token’s signature with Vercel’s public keys, then checks its audience, subject, and claims.

  • Familiarity with the concepts in Workload Identity, and with Vercel’s OIDC docs
  • A Keycard Zone and an Application for the deployment
  • Your Zone issuer URL, from Settings → Connection in Keycard Console
  • The Keycard CLI, signed in with keycard auth signin --org <org-id>
  • A Vercel project with OIDC Federation enabled in Team issuer mode, under the project’s Settings → Security
  • Your team slug, the part of the team URL after vercel.com/, not the team’s display name
  • The project’s ID, as projectId in .vercel/project.json after vercel link

In Team issuer mode, each Vercel team has its own issuer. Create one Provider per team. If the Zone already has one for this issuer, for example from the Console onboarding, use it:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "Vercel <team-slug>",
"identifier": "https://oidc.vercel.com/<team-slug>",
"protocols": {
"oauth2": {
"issuer": "https://oidc.vercel.com/<team-slug>"
}
}
}'

Keycard discovers the team’s signing keys from the issuer.

  1. Add the credential to the Application

    Terminal window
    keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{
    "type": "token",
    "version": 2,
    "application_id": "<application-id>",
    "provider_id": "<vercel-provider-id>",
    "subject": "owner:<team-slug>:project:<project-name>:environment:production",
    "allowed_audience": "https://vercel.com/<team-slug>",
    "authentication_condition": "claims.project_id == \"<project-id>\""
    }'
    Field Value
    subject The project and environment. Match exactly, or end with * for a prefix match: owner:<team-slug>:project:<project-name>:environment:* trusts every environment, including preview deployments and development tokens on developers’ machines.
    allowed_audience https://vercel.com/<team-slug>. Vercel sets aud to it, not to your Zone issuer URL. Every project in your team gets tokens with this audience. To make Keycard the only service that accepts the token, request it with your Zone issuer URL as the audience and omit allowed_audience (see the tip below).
    authentication_condition Pins the project’s ID. A project deleted and recreated with the same name gets a new ID.

    For every claim in a Vercel OIDC token, see Vercel’s OIDC reference.

  2. Copy the credential’s Client ID

    The response’s identifier is the credential’s Client ID. The deployment sends it as client_id. It is not a secret.

  1. Set environment variables

    Add these to the project’s production environment:

    Variable Value
    KEYCARD_ISSUER Your Zone issuer URL
    KEYCARD_CLIENT_ID The credential’s Client ID
    KEYCARD_RESOURCE_URL The Resource the deployment needs a token for

    For example:

    Terminal window
    printf '%s' '<client-id>' | vercel env add KEYCARD_CLIENT_ID production --force

    --force replaces a value that’s already set. Without it, vercel env add fails when the variable exists, so a rerun to change a value stops before the redeploy. Vercel applies environment variables at deploy time, so redeploy after adding them.

  2. Read the Vercel token and exchange it

    Vercel sends the token to each function in the x-vercel-oidc-token request header. Send it as client_assertion, with the credential’s Client ID as client_id, in a client_credentials grant:

    Terminal window
    pip install keycardai-oauth
    import os
    from keycardai.oauth import Client, GrantType
    def get_access_token(vercel_token: str) -> str:
    with Client(os.environ["KEYCARD_ISSUER"]) as client:
    response = client.client_credentials_grant(
    client_assertion=vercel_token,
    client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
    client_id=os.environ["KEYCARD_CLIENT_ID"],
    resource=os.environ["KEYCARD_RESOURCE_URL"],
    )
    return response.access_token
    # In a request handler:
    access_token = get_access_token(request.headers["x-vercel-oidc-token"])
  3. Deploy to production

    Terminal window
    vercel deploy --prod

    Without --prod, Vercel makes a preview deployment, whose tokens carry environment:preview and don’t match the credential’s subject.

Call a function that runs the exchange on the production domain. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.

Keycard returns invalid_client with Invalid client assertion when the issuer, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. vercel env pull writes a development token to .env.local. Decode it to compare its claims against the Provider and credential:

Terminal window
vercel env pull .env.local
grep ^VERCEL_OIDC_TOKEN= .env.local | cut -d= -f2- | tr -d '"' \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub, project_id}'

The development token’s sub ends in environment:development. Production tokens have the same iss, aud, and project_id. Their sub and environment say production, and they have no user_id.

  • iss is https://oidc.vercel.com: the project uses Global issuer mode. Switch it to Team and redeploy.
  • sub doesn’t match: the team segment is the slug, not the display name, and a renamed project carries its new name. Update subject to match.
  • aud doesn’t match: set allowed_audience to the aud in the token.
  • Wrong Client ID: KEYCARD_CLIENT_ID isn’t the Client ID of a Workload Identity credential in this Zone, or KEYCARD_ISSUER names another Zone.