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.
Prerequisites
Section titled “Prerequisites”- 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
projectIdin.vercel/project.jsonaftervercel link
Register your team as a Provider
Section titled “Register your team as a Provider”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:
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.
Create a token credential
Section titled “Create a token credential”-
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 subjectThe 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_audiencehttps://vercel.com/<team-slug>. Vercel setsaudto 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 omitallowed_audience(see the tip below).authentication_conditionPins 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.
-
Copy the credential’s Client ID
The response’s
identifieris the credential’s Client ID. The deployment sends it asclient_id. It is not a secret.
Configure the deployment
Section titled “Configure the deployment”-
Set environment variables
Add these to the project’s production environment:
Variable Value KEYCARD_ISSUERYour Zone issuer URL KEYCARD_CLIENT_IDThe credential’s Client ID KEYCARD_RESOURCE_URLThe Resource the deployment needs a token for For example:
Terminal window printf '%s' '<client-id>' | vercel env add KEYCARD_CLIENT_ID production --force--forcereplaces a value that’s already set. Without it,vercel env addfails 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. -
Read the Vercel token and exchange it
Vercel sends the token to each function in the
x-vercel-oidc-tokenrequest header. Send it asclient_assertion, with the credential’s Client ID asclient_id, in aclient_credentialsgrant:Terminal window pip install keycardai-oauthimport osfrom keycardai.oauth import Client, GrantTypedef 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"])Terminal window npm install @vercel/oidc @keycardai/oauthimport { ClientCredentialsClient } from "@keycardai/oauth";import { getVercelOidcToken } from "@vercel/oidc";const client = new ClientCredentialsClient(process.env.KEYCARD_ISSUER!);export async function getAccessToken(): Promise<string> {const response = await client.requestToken({clientAssertion: await getVercelOidcToken(),clientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",clientId: process.env.KEYCARD_CLIENT_ID!,resource: process.env.KEYCARD_RESOURCE_URL!,});return response.accessToken;}getVercelOidcToken()reads the current request’s header, so call it inside a request handler.Terminal window go get github.com/keycardai/go-sdk/oauthvar keycardClient = oauth.NewClientCredentialsClient(os.Getenv("KEYCARD_ISSUER"))func accessToken(r *http.Request) (string, error) {response, err := keycardClient.RequestToken(r.Context(), oauth.ClientCredentialsRequest{ClientID: os.Getenv("KEYCARD_CLIENT_ID"),ClientAssertion: r.Header.Get("x-vercel-oidc-token"),ClientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",Resource: os.Getenv("KEYCARD_RESOURCE_URL"),})if err != nil {return "", err}return response.AccessToken, nil}Terminal window bundle add keycardai-oauthrequire "keycardai/oauth"CLIENT = Keycardai::OAuth::ClientCredentialsClient.new(issuer: ENV.fetch("KEYCARD_ISSUER"))def access_token(vercel_token)response = CLIENT.request_token(client_id: ENV.fetch("KEYCARD_CLIENT_ID"),client_assertion: vercel_token,client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",resource: ENV.fetch("KEYCARD_RESOURCE_URL"))response.access_tokenend# In a request handler:token = access_token(request["x-vercel-oidc-token"])Vercel sets
VERCEL_OIDC_TOKENduring builds. In a function, send thex-vercel-oidc-tokenrequest header instead.Terminal window curl -sS "$KEYCARD_ISSUER/oauth/2/token" \-d grant_type=client_credentials \-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \--data-urlencode "client_assertion=$VERCEL_OIDC_TOKEN" \--data-urlencode "client_id=$KEYCARD_CLIENT_ID" \--data-urlencode "resource=$KEYCARD_RESOURCE_URL" -
Deploy to production
Terminal window vercel deploy --prodWithout
--prod, Vercel makes a preview deployment, whose tokens carryenvironment:previewand don’t match the credential’s subject.
Verify
Section titled “Verify”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.
Troubleshooting
Section titled “Troubleshooting”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:
vercel env pull .env.localgrep ^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.
issishttps://oidc.vercel.com: the project uses Global issuer mode. Switch it to Team and redeploy.subdoesn’t match: the team segment is the slug, not the display name, and a renamed project carries its new name. Updatesubjectto match.auddoesn’t match: setallowed_audienceto theaudin the token.- Wrong Client ID:
KEYCARD_CLIENT_IDisn’t the Client ID of a Workload Identity credential in this Zone, orKEYCARD_ISSUERnames another Zone.