---
title: "Authentication"
description: "Where the token comes from, in which order, and how to point a provider at a self hosted instance."
canonical_url: "https://forges.agntn.dev/guide/auth"
---
# 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:

<table>
<thead>
  <tr>
    <th>
      Step
    </th>
    
    <th>
      GitHub
    </th>
    
    <th>
      GitLab
    </th>
    
    <th>
      Gitea
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      1. explicit
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      2. env
    </td>
    
    <td>
      <code>
        GH_TOKEN
      </code>
      
      , <code>
        GITHUB_TOKEN
      </code>
    </td>
    
    <td>
      <code>
        GITLAB_TOKEN
      </code>
      
      , <code>
        GL_TOKEN
      </code>
      
      , <code>
        GITLAB_PAT
      </code>
    </td>
    
    <td>
      <code>
        GITEA_TOKEN
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      3. CLI
    </td>
    
    <td>
      <code>
        gh auth token --hostname <host>
      </code>
    </td>
    
    <td>
      <code>
        glab config get token --host <host>
      </code>
    </td>
    
    <td>
      <code>
        tea login helper get
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      4. config file
    </td>
    
    <td>
      <code>
        ~/.config/gh/hosts.yml
      </code>
    </td>
    
    <td>
      <code>
        ~/.config/glab-cli/config.yml
      </code>
    </td>
    
    <td>
      <code>
        ~/.config/tea/config.yml
      </code>
    </td>
  </tr>
</tbody>
</table>

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`](https://github.com/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](https://forges.agntn.dev/guide/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`](https://github.com/agntn/credentials) instead:

<table>
<thead>
  <tr>
    <th>
      Step
    </th>
    
    <th>
      Token
    </th>
    
    <th>
      Account
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      1. explicit
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
    
    <td>
      <code>
        { accountId }
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      2. env
    </td>
    
    <td>
      <code>
        CLOUDFLARE_API_TOKEN
      </code>
    </td>
    
    <td>
      <code>
        AGNTN_CLOUDFLARE_ACCOUNT_ID
      </code>
      
      , <code>
        CLOUDFLARE_ACCOUNT_ID
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      3. CLI
    </td>
    
    <td>
      the <code>
        cf auth login
      </code>
      
       session
    </td>
    
    <td>
      the session, when it reaches exactly one account
    </td>
  </tr>
</tbody>
</table>

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](https://forges.agntn.dev/platforms/artifacts).

## 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.

<table>
<thead>
  <tr>
    <th>
      Step
    </th>
    
    <th>
      Token
    </th>
    
    <th>
      Sent as
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      1. explicit
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
    
    <td>
      Bearer, or Basic with <code>
        { token, username }
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      2. env
    </td>
    
    <td>
      <code>
        BITBUCKET_TOKEN
      </code>
    </td>
    
    <td>
      Bearer
    </td>
  </tr>
  
  <tr>
    <td>
      3. env
    </td>
    
    <td>
      <code>
        BITBUCKET_API_TOKEN
      </code>
      
       and <code>
        BITBUCKET_EMAIL
      </code>
    </td>
    
    <td>
      Basic, with the email as the login
    </td>
  </tr>
</tbody>
</table>

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](https://forges.agntn.dev/platforms/bitbucket).

## 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](https://forges.agntn.dev/platforms/gitee).

## Azure DevOps

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

<table>
<thead>
  <tr>
    <th>
      Step
    </th>
    
    <th>
      Token
    </th>
    
    <th>
      Organization
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      1. Explicit
    </td>
    
    <td>
      <code>
        { token }
      </code>
    </td>
    
    <td>
      <code>
        baseURL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      2. Environment
    </td>
    
    <td>
      <code>
        AZURE_DEVOPS_EXT_PAT
      </code>
    </td>
    
    <td>
      <code>
        AZURE_DEVOPS_EXT__DEFAULTS_ORGANIZATION
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      3. Config
    </td>
    
    <td>
      PAT file saved by <code>
        az devops login
      </code>
    </td>
    
    <td>
      <code>
        [defaults] organization
      </code>
      
       in the <code>
        az devops
      </code>
      
       config
    </td>
  </tr>
</tbody>
</table>

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](https://forges.agntn.dev/platforms/azure-devops).

## 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](https://forges.agntn.dev/platforms/gitcafe).

## 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](https://forges.agntn.dev/platforms/pushin).

## 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](https://forges.agntn.dev/platforms/tangled).

## 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

<table>
<thead>
  <tr>
    <th>
      Platform
    </th>
    
    <th>
      Header
    </th>
    
    <th>
      Value
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      GitHub, GitBucket
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        token <token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      GitLab
    </td>
    
    <td>
      <code>
        Private-Token
      </code>
    </td>
    
    <td>
      <code>
        <token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Gitea, Forgejo
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        token <token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Bitbucket
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Bearer <token>
      </code>
      
      , or <code>
        Basic
      </code>
      
       with an API token and its email
    </td>
  </tr>
  
  <tr>
    <td>
      Gitee
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        token <token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Azure DevOps
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Basic
      </code>
      
       with an empty login and the PAT
    </td>
  </tr>
  
  <tr>
    <td>
      GitCafe
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Bearer <gct_ token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Pushin.eu
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Bearer <token>
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Tangled
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Bearer
      </code>
      
       with a PDS session from the app password
    </td>
  </tr>
  
  <tr>
    <td>
      Artifacts
    </td>
    
    <td>
      <code>
        Authorization
      </code>
    </td>
    
    <td>
      <code>
        Bearer <token>
      </code>
    </td>
  </tr>
</tbody>
</table>

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:

<table>
<thead>
  <tr>
    <th>
      Platform
    </th>
    
    <th>
      Variable
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      GitHub, GitBucket
    </td>
    
    <td>
      <code>
        FORGES_GITHUB_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      GitLab
    </td>
    
    <td>
      <code>
        FORGES_GITLAB_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Gitea, Forgejo
    </td>
    
    <td>
      <code>
        FORGES_GITEA_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Bitbucket
    </td>
    
    <td>
      <code>
        FORGES_BITBUCKET_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Gitee
    </td>
    
    <td>
      <code>
        FORGES_GITEE_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Azure DevOps
    </td>
    
    <td>
      <code>
        FORGES_AZURE_DEVOPS_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      GitCafe
    </td>
    
    <td>
      <code>
        FORGES_GITCAFE_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Pushin.eu
    </td>
    
    <td>
      <code>
        FORGES_PUSHIN_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Tangled
    </td>
    
    <td>
      <code>
        FORGES_TANGLED_BASE_URL
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Artifacts
    </td>
    
    <td>
      <code>
        FORGES_ARTIFACTS_BASE_URL
      </code>
    </td>
  </tr>
</tbody>
</table>

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.


## Sitemap

See the full [sitemap](https://forges.agntn.dev/sitemap.md) for all pages.
