Guide

Authentication

Where the token comes from, in which order, and how to point a provider at a self hosted instance.

The chain

createProvider(platform, config?) looks for a token in four places and stops at the first hit:

StepGitHubGitLabGitea
1. explicit{ token }{ token }{ token }
2. envGH_TOKEN, GITHUB_TOKENGITLAB_TOKEN, GL_TOKEN, GITLAB_PATGITEA_TOKEN
3. CLIgh auth token --hostname <host>glab config get token --host <host>tea login helper get
4. config file~/.config/gh/hosts.yml~/.config/glab-cli/config.yml~/.config/tea/config.yml

Steps 3 and 4 use the hostname from baseURL, or the platform default. So a GitHub Enterprise baseURL asks gh for that host's token, not for github.com's. Small thing, saves a confusing 401.

The chain itself lives in @agntn/credentials now. One hunt, not a copy in every library. Why does it start gh and glab without your token variables? Because glab config get token prints GITLAB_TOKEN for whatever host you ask about. That's step 2 answering again, with the wrong host attached. Env already had its turn.

When nothing matches, createProvider throws AuthenticationError and tells you which env var to set and which login to run. It never goes anonymous behind your back.

ts
import { resolveToken } from "@agntn/forges";

const found = resolveToken("github"); // { token, source: "env", origin: "GH_TOKEN" } or null

resolveToken is the same chain without a provider. Handy as a preflight check. source says which step won, origin says where exactly: GL_TOKEN, glab, gh hosts.yml.

resolveToken("github", { account: "octocat" }) asks gh auth token --user=octocat, then that login's entry under users in hosts.yml, and never falls back to the active account. The env step still comes first though, so a token found this way is a candidate, not proof. users.authenticated() says whose it really is, and the agent tools check it once per account before the first write and pin the result, same as the default credential.

Anonymous on purpose

An empty string is a real token here. It means make unauthenticated calls:

ts
const anonymous = await createProvider("github", { token: "" });
await anonymous.repos.get({ owner: "nitrojs", repo: "nitro" }); // public data, 60 requests an hour per address

The check inside is token !== undefined, not a falsy check. "" is a decision, undefined means go look. The agent tools use exactly this for reads when no credential exists, see Agents.

Cloudflare Artifacts

Artifacts doesn't fit the table above. It wants a Cloudflare API token plus the account that owns the namespaces, and it has no guest mode at all. So createProvider("artifacts") asks @agntn/credentials/cloudflare instead:

StepTokenAccount
1. explicit{ token }{ accountId }
2. envCLOUDFLARE_API_TOKENAGNTN_CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_ACCOUNT_ID
3. CLIthe cf auth login sessionthe session, when it reaches exactly one account

A token from steps 1 or 2 needs an account from those steps too. { token: "" } isn't anonymous here, it means go look. The session token expires, so the provider asks the chain again on every request instead of keeping one token forever. Details on the Artifacts page.

Bitbucket

Bitbucket Cloud has no CLI login to read, so its chain stops at the environment. It also says workspace where everyone else says owner. Same field here, owner, Bitbucket's word in your head.

StepTokenSent as
1. explicit{ token }Bearer, or Basic with { token, username }
2. envBITBUCKET_TOKENBearer
3. envBITBUCKET_API_TOKEN and BITBUCKET_EMAILBasic, with the email as the login

An API token without its email isn't a token yet, so step 3 skips it. { token: "" } reads public repositories, until Bitbucket answers 429 a handful of requests in. Details on the Bitbucket page.

Gitee

Gitee has no CLI login either. The chain is an explicit { token }, then GITEE_TOKEN, then GITEE_ACCESS_TOKEN. Short and boring, the good kind.

{ token: "" } reads public repositories until Gitee's firewall answers a plain-text 403, a few dozen requests in. forges calls that a rate limit, because it is one. Details on the Gitee page.

Azure DevOps

Where does forges find your token and organization? It checks these sources in order.

StepTokenOrganization
1. Explicit{ token }baseURL
2. EnvironmentAZURE_DEVOPS_EXT_PATAZURE_DEVOPS_EXT__DEFAULTS_ORGANIZATION
3. ConfigPAT file saved by az devops login[defaults] organization in the az devops config

The token and organization chains are independent. The PAT file is ~/.azure/azuredevops/personalAccessTokens on Linux. The keyring isn't checked.

The PAT uses Basic auth with an empty username: Authorization: Basic base64(":<PAT>"). Azure DevOps gets a username field, just not a name.

Pass { token: "" } to read public projects anonymously. They exist, dnceng-public/public is one, but most organizations keep everything behind the PAT.

Details on the Azure DevOps page.

GitCafe

Grab a gct_ personal access token from GitCafe Settings, Tokens. A GitCafe MCP token looks just as official and doesn't work here, so take the right one.

forges checks an explicit { token }, then CAFE_TOKEN. Next comes the cafe CLI's login helper, cafe auth http credential get. Last comes gitcafe/credentials.json under the XDG config directory. An expired saved login is skipped.

cafe auth login saves one. { token: "" } reads public repositories anonymously, and only users.authenticated insists on a token.

Details on the GitCafe page.

Pushin.eu

Get a personal access token under Settings, Security. Pushin.eu wants two factor auth on first.

forges checks { token }, then PUSHIN_TOKEN. Last comes the login saved by pun login in pun/hosts.json. Tokens go out as Bearer.

Public repositories need no token. { token: "" } reads them anonymously, and users.authenticated insists on a token.

Details on the Pushin.eu page.

Tangled

No token here. Tangled signs in with an atproto app password and the handle or DID it belongs to.

forges checks { username, token }, then TANGLED_HANDLE with TANGLED_PASSWORD. Half a pair finds nothing. The session from the account's PDS goes out as Bearer.

Public repositories read with { token: "" }. Writes and users.authenticated want the password.

Details on the Tangled page.

Explicit token and host

ts
const github = await createProvider("github", { token: process.env.GITHUB_TOKEN });

const gitlab = await createProvider("gitlab", {
  token: "glpat-…",
  baseURL: "https://gitlab.example.com", // /api/v4 is added when missing
});

const gitea = await createProvider("gitea", {
  baseURL: "https://codeberg.org", // /api/v1 is added when missing
});

const gitbucket = await createProvider("github", {
  token: "…",
  baseURL: "https://gitbucket.example.com/api/v3",
});

GitLab and Gitea normalize the base URL, so the bare host works. GitHub takes the URL as given, so GitBucket and GitHub Enterprise need /api/v3 spelled out.

Headers

PlatformHeaderValue
GitHub, GitBucketAuthorizationtoken <token>
GitLabPrivate-Token<token>
Gitea, ForgejoAuthorizationtoken <token>
BitbucketAuthorizationBearer <token>, or Basic with an API token and its email
GiteeAuthorizationtoken <token>
Azure DevOpsAuthorizationBasic with an empty login and the PAT
GitCafeAuthorizationBearer <gct_ token>
Pushin.euAuthorizationBearer <token>
TangledAuthorizationBearer with a PDS session from the app password
ArtifactsAuthorizationBearer <token>

You never set these. They are here so a proxy log makes sense.

Self hosted endpoints for agents

The MCP server and the extensions read one more variable per platform from the process environment:

PlatformVariable
GitHub, GitBucketFORGES_GITHUB_BASE_URL
GitLabFORGES_GITLAB_BASE_URL
Gitea, ForgejoFORGES_GITEA_BASE_URL
BitbucketFORGES_BITBUCKET_BASE_URL
GiteeFORGES_GITEE_BASE_URL
Azure DevOpsFORGES_AZURE_DEVOPS_BASE_URL
GitCafeFORGES_GITCAFE_BASE_URL
Pushin.euFORGES_PUSHIN_BASE_URL
TangledFORGES_TANGLED_BASE_URL
ArtifactsFORGES_ARTIFACTS_BASE_URL

The value is the full API base URL. It is never a tool argument, so a model cannot point a call at another host. And a failure message never repeats the endpoint, so the host stays out of the model's context even when the platform answers with an error.

What a token buys

  • The cache is keyed by base URL and a hash of the token. Two providers in one process never read each other's answers.
  • users.authenticated() says who the token is.
  • Writes need one on every platform: issues.create, issues.update, issues.createComment, pullRequests.create, pullRequests.update, pullRequests.merge, pullRequests.dismissReview, pullRequests.createComment, the updateComment and deleteComment of issues, pull requests and threads, threads.reply, threads.resolve, threads.unresolve. issues.transfer too, on GitHub and GitLab, the two forges that move issues.
  • GitHub review threads go through GraphQL and GraphQL has no anonymous access. GitLab discussions answer 401 without a token, even on a public project, which surprised me. Gitea review comments read fine anonymously on public repos.