Skip to content
Source
Sign inSign up

Call Claude

CI / Secrets and trust

Call Claude with OIDC

Your CI job calls the Claude API, and an Anthropic API key that you save as a CI secret works until you rotate it. With OIDC, the job trades a 5-minute token from the forge with Anthropic for a short-lived API token, and the forge holds no Anthropic key.

3 min read

This page is for a CI job. For the agents of the software factory, see Use Anthropic with no stored key.

You need:

  • Write access to the repository on TanStack Source, and a workflow. Your first workflow shows how to write one.
  • The admin role in your Anthropic organization.

Note

The steps in the Claude Console follow Anthropic's guide to Workload Identity Federation. If the Console looks different, follow that guide with the values of this page.

1. Add the forge as an issuer

In the Claude Console, open Workload identity and select Connect workload. Pick Custom OIDC, and give the issuer URL https://source.tanstack.com/oidc. Anthropic finds the public keys by discovery.

Note

The issuer URL is the address of the site, then /oidc. On another deployment of TanStack Source, use its address.

2. Make a rule that trusts your branch

In the same wizard, make a service account and a federation rule. For runs of main in acme/app:

{
  "name": "source-ci-main",
  "issuer_id": "fdis_...",
  "match": {
    "subject_prefix": "repo:acme/app:ref:refs/heads/main",
    "audience": "https://api.anthropic.com"
  },
  "target": { "type": "service_account", "service_account_id": "svac_..." },
  "workspace_id": "wrkspc_...",
  "oauth_scope": "workspace:developer",
  "token_lifetime_seconds": 600
}
  • The subject names the repository and the ref, with no *. With this rule, only runs of main in acme/app get a token.
  • A subject of repo:acme/* alone matches every repository of acme, and its pull request runs.

Copy the id of the rule (fdrl_...) and the id of your Anthropic organization.

Important

Give main a branch rule that requires a pull request: src ci rule set main --require-pull. Without it, a person with write access can push code to main alone, and that code gets a token.

3. Ask for the token in the job

Add permissions: { idToken: true } to the job, and api.anthropic.com to its egress. Write the token to a file, and name the file and the ids in the env:

import { defineWorkflow, push, setup } from '@tanstack-source/ci'

export default defineWorkflow('ask-claude', { on: [push({ branches: ['main'] })] }).job('ask-claude', {
  permissions: { idToken: true },
  env: {
    ANTHROPIC_FEDERATION_RULE_ID: 'fdrl_01ABC',
    ANTHROPIC_ORGANIZATION_ID: '00000000-0000-0000-0000-000000000000',
    ANTHROPIC_IDENTITY_TOKEN_FILE: '/tmp/anthropic-identity-token',
  },
  egress: ['registry.npmjs.org', 'api.anthropic.com'],
  steps: [
    setup.pnpm(),
    { name: 'Install', run: 'pnpm install --frozen-lockfile' },
    { name: 'Token', run: 'curl -fsS "$TANSTACK_SOURCE_OIDC_REQUEST_URL?audience=https://api.anthropic.com" | jq -r .token > /tmp/anthropic-identity-token' },
    { name: 'Ask Claude', run: 'pnpm run ask-claude' },
  ],
})
  • pnpm run ask-claude is your own script. The Anthropic SDKs read the three variables, and trade the token with Anthropic by themselves. Do not set ANTHROPIC_API_KEY: it wins over the federation.
  • The ids are not secrets, so they can be in the workflow file.
  • The token file is in /tmp, outside the workspace, so no artifact takes it.

4. Push to main

git add .source/workflows/ask-claude.ts
git commit -m "ci: call Claude with OIDC"
git push origin main

Done

Open the run on the CI tab. The Ask Claude step passes. The job called Claude, and the forge holds no Anthropic key.

A job that calls Claude for more than 10 minutes

The token of the forge is good for 5 minutes, and for one trade. The API token that Anthropic gives lives at most 10 minutes. The SDK then reads the file again for a new trade.

So a long job must write a new token to the file before the SDK needs it. Run the curl command of the Token step again, for example before each call of your script.

When the trade fails

Anthropic answers every refusal with 401 and Authentication failed. The reason is in the Claude Console: Workload identity, then the authentication history.

CauseFix
The run gets no tokenSee Which runs get a token.
The subject does not matchRead the sub of your run in The claims of a token. A pull request run has repo:<owner>/<repo>:pull_request.
The request never reaches AnthropicAdd api.anthropic.com to the egress of the job.
The token was used beforeEach token works for one trade. Ask for a new one.