|
| 1 | +--- |
| 2 | +title: Authenticating with Copilot SDK |
| 3 | +shortTitle: Authenticate Copilot SDK |
| 4 | +intro: 'Choose the authentication method in {% data variables.copilot.copilot_sdk %} that best fits your deployment scenario.' |
| 5 | +product: '{% data reusables.gated-features.copilot-sdk %}' |
| 6 | +versions: |
| 7 | + feature: copilot |
| 8 | +contentType: how-tos |
| 9 | +category: |
| 10 | + - Configure Copilot |
| 11 | +--- |
| 12 | + |
| 13 | +{% data reusables.copilot.copilot-sdk.technical-preview-note %} |
| 14 | + |
| 15 | +## Authentication methods overview |
| 16 | + |
| 17 | +{% data variables.copilot.copilot_sdk %} supports multiple authentication methods to fit different use cases. |
| 18 | + |
| 19 | +| Method | Use case | {% data variables.product.prodname_copilot_short %} subscription required | |
| 20 | +|--------|----------|-------------------------------| |
| 21 | +| [GitHub signed-in user](#github-signed-in-user) | Interactive apps where users sign in with {% data variables.product.github %} | Yes | |
| 22 | +| [OAuth {% data variables.product.github %} App](#oauth-github-app) | Apps acting on behalf of users via OAuth | Yes | |
| 23 | +| [Environment variables](#environment-variables) | CI/CD, automation, server-to-server | Yes | |
| 24 | +| [BYOK (bring your own key)](#byok-bring-your-own-key) | Using your own API keys (Azure AI Foundry, OpenAI, etc) | No | |
| 25 | + |
| 26 | +## GitHub signed-in user |
| 27 | + |
| 28 | +This is the default authentication method when running the {% data variables.copilot.copilot_cli %} interactively, see [AUTOTITLE](/copilot/how-tos/copilot-cli/set-up-copilot-cli/authenticate-copilot-cli). Users authenticate via the {% data variables.product.github %} OAuth device flow, and the SDK uses their stored credentials. |
| 29 | + |
| 30 | +**How it works:** |
| 31 | +1. User runs the `copilot` CLI and signs in via {% data variables.product.github %} OAuth. |
| 32 | +1. Credentials are stored securely in the system keychain. |
| 33 | +1. The SDK automatically uses stored credentials. |
| 34 | + |
| 35 | +**SDK configuration:** |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { CopilotClient } from "@github/copilot-sdk"; |
| 39 | + |
| 40 | +// Default: uses signed-in user credentials |
| 41 | +const client = new CopilotClient(); |
| 42 | +``` |
| 43 | + |
| 44 | +For examples in other languages, see [Authentication](https://github.com/github/copilot-sdk/blob/main/docs/auth/index.md#github-signed-in-user) in the `github/copilot-sdk` repository. |
| 45 | + |
| 46 | +**When to use this method:** |
| 47 | + |
| 48 | +* Desktop applications where users interact directly |
| 49 | +* Development and testing environments |
| 50 | +* Any scenario where a user can sign in interactively |
| 51 | + |
| 52 | +## OAuth GitHub App |
| 53 | + |
| 54 | +Use an OAuth {% data variables.product.github %} App to authenticate users through your application and pass their credentials to the SDK. This lets your application make {% data variables.product.prodname_copilot %} API requests on behalf of users who authorize your app. |
| 55 | + |
| 56 | +**How it works:** |
| 57 | +1. User authorizes your OAuth {% data variables.product.github %} App. |
| 58 | +1. Your app receives a user access token (`gho_` or `ghu_` prefix). |
| 59 | +1. Pass the token to the SDK via the `githubToken` option. |
| 60 | + |
| 61 | +**SDK configuration:** |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { CopilotClient } from "@github/copilot-sdk"; |
| 65 | + |
| 66 | +const client = new CopilotClient({ |
| 67 | + githubToken: userAccessToken, // Token from OAuth flow |
| 68 | + useLoggedInUser: false, // Don't use stored CLI credentials |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +For examples in other languages, see [Authentication](https://github.com/github/copilot-sdk/blob/main/docs/auth/index.md#oauth-github-app) in the `github/copilot-sdk` repository. |
| 73 | + |
| 74 | +**Supported token types:** |
| 75 | + |
| 76 | +* `gho_` — OAuth user access tokens |
| 77 | +* `ghu_` — {% data variables.product.github %} App user access tokens |
| 78 | +* `github_pat_` — {% data variables.product.pat_v2_caps_plural %} |
| 79 | + |
| 80 | +**Not supported:** |
| 81 | + |
| 82 | +* `ghp_` — {% data variables.product.pat_v1_caps_plural %} (closing down) |
| 83 | + |
| 84 | +**When to use this method:** |
| 85 | + |
| 86 | +* Web applications where users sign in via {% data variables.product.github %} |
| 87 | +* Software-as-a-service (SaaS) applications building on top of {% data variables.product.prodname_copilot %} |
| 88 | +* Any multi-user application where you need to make requests on behalf of different users |
| 89 | + |
| 90 | +## Environment variables |
| 91 | + |
| 92 | +For automation, CI/CD pipelines, and server-to-server scenarios, you can authenticate using environment variables. |
| 93 | + |
| 94 | +**Supported environment variables (in priority order):** |
| 95 | + |
| 96 | +1. `COPILOT_GITHUB_TOKEN` — Recommended for explicit {% data variables.product.prodname_copilot_short %} usage |
| 97 | +1. `GH_TOKEN` — {% data variables.product.prodname_cli %} compatible |
| 98 | +1. `GITHUB_TOKEN` — {% data variables.product.prodname_actions %} compatible |
| 99 | + |
| 100 | +The SDK automatically detects and uses these environment variables without any code changes required: |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { CopilotClient } from "@github/copilot-sdk"; |
| 104 | + |
| 105 | +// Token is read from environment variable automatically |
| 106 | +const client = new CopilotClient(); |
| 107 | +``` |
| 108 | + |
| 109 | +**When to use this method:** |
| 110 | + |
| 111 | +* CI/CD pipelines ({% data variables.product.prodname_actions %}, Jenkins, etc) |
| 112 | +* Automated testing |
| 113 | +* Server-side applications with service accounts |
| 114 | +* Development when you don't want to use interactive sign-in |
| 115 | + |
| 116 | +## BYOK (bring your own key) |
| 117 | + |
| 118 | +BYOK lets you use your own API keys from model providers like Azure AI Foundry, OpenAI, or Anthropic. This bypasses {% data variables.product.prodname_copilot %} authentication entirely. |
| 119 | + |
| 120 | +**Key benefits:** |
| 121 | + |
| 122 | +* No {% data variables.product.prodname_copilot %} subscription required |
| 123 | +* Use enterprise model deployments |
| 124 | +* Direct billing with your model provider |
| 125 | +* Support for Azure AI Foundry, OpenAI, Anthropic, and OpenAI-compatible endpoints |
| 126 | + |
| 127 | +For complete setup instructions, including provider configuration options, limitations, and code examples, see [AUTOTITLE](/copilot/how-tos/copilot-sdk/authenticate-copilot-sdk/bring-your-own-key). |
| 128 | + |
| 129 | +## Authentication priority |
| 130 | + |
| 131 | +When multiple authentication methods are available, the SDK uses them in this priority order: |
| 132 | + |
| 133 | +1. **Explicit `githubToken`** — Token passed directly to the SDK constructor |
| 134 | +1. **HMAC key** — `CAPI_HMAC_KEY` or `COPILOT_HMAC_KEY` environment variables |
| 135 | +1. **Direct API token** — `GITHUB_COPILOT_API_TOKEN` with `COPILOT_API_URL` |
| 136 | +1. **Environment variable tokens** — `COPILOT_GITHUB_TOKEN` → `GH_TOKEN` → `GITHUB_TOKEN` |
| 137 | +1. **Stored OAuth credentials** — From previous `copilot` CLI sign-in |
| 138 | +1. **{% data variables.product.prodname_cli %}** — `gh auth` credentials |
| 139 | + |
| 140 | +## Disabling auto sign-in |
| 141 | + |
| 142 | +To prevent the SDK from automatically using stored credentials or {% data variables.product.prodname_cli %} authentication, set the `useLoggedInUser` option to `false`: |
| 143 | + |
| 144 | +```typescript |
| 145 | +const client = new CopilotClient({ |
| 146 | + useLoggedInUser: false, // Only use explicit tokens |
| 147 | +}); |
| 148 | +``` |
| 149 | + |
| 150 | +For examples in other languages, see [Authentication](https://github.com/github/copilot-sdk/blob/main/docs/auth/index.md#disabling-auto-login) in the `github/copilot-sdk` repository. |
| 151 | + |
| 152 | +## Next steps |
| 153 | + |
| 154 | +* [AUTOTITLE](/copilot/how-tos/copilot-sdk/authenticate-copilot-sdk/bring-your-own-key) |
| 155 | +* [MCP servers documentation](https://github.com/github/copilot-sdk/blob/main/docs/features/mcp.md)—Connect to external tools using the SDK |
0 commit comments