# How the Copilot SDK Handles Authentication for Enterprise Use: 4 Methods Explained

> Discover 4 authentication methods for Copilot SDK in enterprise use GitHub OAuth per-session tokens environment-variable fallbacks BYOK and priority hierarchy

- Repository: [GitHub/copilot-sdk](https://github.com/github/copilot-sdk)
- Tags: how-to-guide
- Published: 2026-07-18

---

**The Copilot SDK supports enterprise authentication through GitHub OAuth flows, per-session tokens for multi-tenant services, environment-variable fallbacks for automation, and BYOK (Bring Your Own Key) for air-gapped environments, with a strict priority hierarchy that prioritizes explicit tokens over stored credentials.**

The GitHub Copilot SDK provides flexible authentication mechanisms designed for enterprise deployments, from cloud-based GitHub Enterprise Cloud organizations to isolated on-premises environments. Whether you are building a multi-tenant service or automating CI/CD pipelines, understanding how the SDK handles credentials is essential for secure integration. This guide examines the authentication architecture as implemented in the `github/copilot-sdk` repository, covering priority logic, enterprise-specific features, and practical implementation patterns.

## Authentication Methods for Enterprise Deployments

The SDK offers four distinct authentication strategies to accommodate different enterprise security models and network configurations.

### GitHub Signed-In User (Default)

The default method leverages the bundled Copilot CLI credentials stored on the host machine. When a user runs `copilot auth login`, the OAuth token is stored locally, and the SDK automatically picks up these credentials when initializing `CopilotClient` without explicit parameters.

For enterprise organizations using GitHub Enterprise Cloud or GitHub Enterprise Server, this method automatically respects organizational policies including SAML SSO, IP allowlists, and mandatory MFA requirements. No additional code is required to enforce these policies—the OAuth token inherently carries the enterprise membership context.

### OAuth GitHub App (Recommended for Multi-Tenant Services)

For applications serving multiple enterprise tenants, the recommended approach involves obtaining individual user access tokens (`gho_` or `ghu_`) through an OAuth flow and passing them explicitly to the SDK. In [`nodejs/src/client.ts`](https://github.com/github/copilot-sdk/blob/main/nodejs/src/client.ts), the constructor accepts a `gitHubToken` option that overrides all other credential sources.

This method ensures that each session operates under the correct enterprise identity, respecting per-user permissions and organizational boundaries. When using this approach, set `useLoggedInUser: false` to prevent the SDK from falling back to the host's CLI credentials.

### Environment Variables (CI/CD Automation)

For server-to-server automation and continuous integration pipelines, the SDK falls back to standard environment variables when no explicit token is provided. The SDK checks for `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, and `GITHUB_TOKEN` in that order.

This approach is ideal for enterprise automation scenarios where service accounts or personal access tokens are injected securely into the environment without hardcoding credentials in source control.

### BYOK (Bring Your Own Key) for Air-Gapped Environments

For enterprises with strict network isolation or data residency requirements, the SDK supports BYOK configuration that bypasses GitHub authentication entirely. Instead of GitHub Copilot credentials, you supply API keys for external LLM providers such as OpenAI, Azure AI Foundry, or Anthropic.

According to [`docs/auth/byok.md`](https://github.com/github/copilot-sdk/blob/main/docs/auth/byok.md), this mode is designed for on-premises or air-gapped deployments where external internet access is blocked. The SDK communicates directly with the specified provider using the provided credentials.

## Authentication Priority Hierarchy

When multiple credential sources are present, the SDK selects the most explicit option first, following a strict priority order documented in [`docs/auth/authenticate.md`](https://github.com/github/copilot-sdk/blob/main/docs/auth/authenticate.md):

1. **Explicit SDK token** (`gitHubToken` option) – highest priority
2. **Direct Copilot API environment auth** (`COPILOT_GITHUB_TOKEN`)
3. **Environment-variable GitHub tokens** (`GH_TOKEN`, `GITHUB_TOKEN`)
4. **Stored Copilot CLI credentials** (signed-in user)
5. **GitHub CLI credentials** (`gh auth login`)

This hierarchy ensures that programmatically provided tokens always take precedence over ambient credentials, preventing accidental privilege escalation in multi-user environments.

## Enterprise-Ready Authentication Features

Beyond basic credential management, the SDK includes specific features for enterprise security and compliance requirements.

### Per-Session Tokens for Multi-User Services

The SDK supports per-session authentication, allowing you to create isolated client instances for different enterprise users within the same application process. This pattern is validated in [`nodejs/test/e2e/per_session_auth.e2e.test.ts`](https://github.com/github/copilot-sdk/blob/main/nodejs/test/e2e/per_session_auth.e2e.test.ts), which demonstrates creating clients with distinct tokens for concurrent users.

```typescript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient({
  gitHubToken: enterpriseUserToken, // Token obtained via OAuth or PAT
  useLoggedInUser: false,          // Prevent fallback to host CLI
});

```

Each client instance maintains its own authentication context, ensuring that requests are executed under the correct enterprise identity without cross-contamination between sessions.

### Managed Settings Enforcement

For enterprises utilizing GitHub's "Managed Settings" (enforcing SAML SSO, IP allowlists, or MFA), the SDK forwards an `enableManagedSettings` flag during session creation. As noted in [`CHANGELOG.md`](https://github.com/github/copilot-sdk/blob/main/CHANGELOG.md), this flag instructs the Copilot service to honor organizational policies when processing requests.

This feature ensures that SDK-based applications comply with enterprise governance policies automatically, without requiring manual policy checks in application code.

### BYOK for Air-Gapped Deployments

The BYOK implementation in [`docs/auth/byok.md`](https://github.com/github/copilot-sdk/blob/main/docs/auth/byok.md) supports enterprise-controlled model providers, allowing organizations to maintain full control over billing, data residency, and network routing. Common configurations include Azure AI Foundry deployments for enterprises requiring private endpoints.

```typescript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient({
  provider: "openai",
  apiKey: process.env.OPENAI_API_KEY,
  model: "gpt-4o-mini",
});

```

## Implementation Examples

### Default Signed-In User Authentication

```typescript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();   // Picks up logged-in user's GitHub token
await client.start();

```

The client reads the stored OAuth token from the Copilot CLI, which already carries any enterprise SSO or IP-allowlist restrictions established during the initial login.

### Multi-Tenant Enterprise Service

```typescript
import { CopilotClient } from "@github/copilot-sdk";

async function createEnterpriseClient(userAccessToken: string) {
  const client = new CopilotClient({
    gitHubToken: userAccessToken,
    useLoggedInUser: false,
  });
  await client.start();
  return client;
}

```

Pass a distinct token for each enterprise user; the SDK uses this token for all subsequent RPC calls, maintaining proper isolation between tenants.

### CI/CD Pipeline Authentication

```bash
export GH_TOKEN=ghp_*********************
node my-script.js

```

```typescript
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();   // Automatically picks up GH_TOKEN
await client.start();

```

## Summary

- **Four authentication methods** support diverse enterprise needs: signed-in user, OAuth GitHub App tokens, environment variables, and BYOK for air-gapped networks.
- **Strict priority hierarchy** ensures explicit tokens (`gitHubToken`) override ambient credentials, preventing security leaks in multi-user scenarios.
- **Per-session tokens** enable multi-tenant SaaS applications to serve multiple enterprise customers concurrently without credential mixing.
- **Managed settings enforcement** automatically respects GitHub Enterprise policies including SAML SSO and IP allowlists when the `enableManagedSettings` flag is enabled.
- **BYOK support** allows deployment in isolated environments without GitHub internet connectivity, using enterprise-controlled LLM provider keys.

## Frequently Asked Questions

### How does the Copilot SDK prioritize multiple authentication sources?

The SDK evaluates credentials in a specific order: explicit `gitHubToken` constructor options take highest priority, followed by `COPILOT_GITHUB_TOKEN`, then `GH_TOKEN` or `GITHUB_TOKEN` environment variables, then stored Copilot CLI credentials, and finally GitHub CLI credentials. This hierarchy is implemented in the authentication resolution logic to ensure programmatic tokens always supersede ambient credentials.

### What is the recommended authentication method for multi-tenant enterprise services?

For multi-tenant applications serving multiple enterprise customers, use the **OAuth GitHub App** method. Obtain individual user access tokens through the OAuth flow and pass them to the `CopilotClient` constructor using the `gitHubToken` option with `useLoggedInUser: false`. This pattern, demonstrated in [`nodejs/test/e2e/per_session_auth.e2e.test.ts`](https://github.com/github/copilot-sdk/blob/main/nodejs/test/e2e/per_session_auth.e2e.test.ts), ensures each tenant's requests execute under their specific enterprise identity and permissions.

### Can the Copilot SDK operate in air-gapped enterprise environments without internet access?

Yes, through **BYOK (Bring Your Own Key)** authentication. By configuring the client with `provider` and `apiKey` options pointing to internal or enterprise-controlled LLM endpoints (such as Azure AI Foundry), the SDK bypasses GitHub authentication entirely. This mode requires no GitHub tokens and routes all inference requests to the specified provider, as documented in [`docs/auth/byok.md`](https://github.com/github/copilot-sdk/blob/main/docs/auth/byok.md).

### Does the Copilot SDK support GitHub Enterprise Server authentication?

Yes, the SDK supports GitHub Enterprise Server through the standard GitHub signed-in user and OAuth GitHub App methods. When users authenticate via `copilot auth login` or OAuth against a GitHub Enterprise Server instance, the resulting tokens carry the enterprise server context. The SDK respects organizational SAML SSO and IP-allowlist policies enforced by the enterprise server without requiring additional SDK configuration.