diff --git a/docs/auth/authenticate.md b/docs/auth/authenticate.md index d54c954517..2fc80dd0aa 100644 --- a/docs/auth/authenticate.md +++ b/docs/auth/authenticate.md @@ -7,7 +7,7 @@ The GitHub Copilot SDK supports multiple authentication methods to fit different | Method | Use Case | Copilot Subscription Required | |--------|----------|-------------------------------| | [GitHub Signed-in User](#github-signed-in-user) | Interactive apps where users sign in with GitHub | Yes | -| [OAuth GitHub App](#oauth-github-app) | Apps acting on behalf of users via OAuth | Yes | +| [GitHub OAuth App](#github-oauth-app) | Apps acting on behalf of users via OAuth | Yes | | [Environment Variables](#environment-variables) | CI/CD, automation, server-to-server | Yes | | [Server-to-server authentication](./server-to-server-tokens.md) | Organization-attributed automation and direct organization billing | No user subscription; organization policy required | | [BYOK (Bring Your Own Key)](./byok.md) | Using your own API keys (Microsoft Foundry, OpenAI, and more) | No | @@ -24,26 +24,13 @@ This is the default authentication method when running the Copilot CLI interacti **SDK Configuration:**
-Node.js / TypeScript +.NET -```typescript -import { CopilotClient } from "@github/copilot-sdk"; +```csharp +using GitHub.Copilot; // Default: uses logged-in user credentials -const client = new CopilotClient(); -``` - -
- -
-Python - -```python -from copilot import CopilotClient - -# Default: uses logged-in user credentials -client = CopilotClient() -await client.start() +await using CopilotClient client = new(); ```
@@ -75,26 +62,51 @@ client := copilot.NewClient(nil)
-.NET +Java -```csharp -using GitHub.Copilot; +```java +import com.github.copilot.CopilotClient; // Default: uses logged-in user credentials -await using var client = new CopilotClient(); +var client = new CopilotClient(); +client.start().get(); ```
-Java +Python -```java -import com.github.copilot.CopilotClient; +```python +from copilot import CopilotClient + +# Default: uses logged-in user credentials +client = CopilotClient() +await client.start() +``` + +
+ +
+Rust + +```rust +use github_copilot_sdk::{Client, ClientOptions}; // Default: uses logged-in user credentials -var client = new CopilotClient(); -client.start().get(); +let client = Client::start(ClientOptions::default()).await?; +``` + +
+ +
+TypeScript + +```typescript +import { CopilotClient } from "@github/copilot-sdk"; + +// Default: uses logged-in user credentials +const client = new CopilotClient(); ```
@@ -104,42 +116,41 @@ client.start().get(); * Development and testing environments * Any scenario where a user can sign in interactively -## OAuth GitHub App +## GitHub OAuth App Use an OAuth GitHub App to authenticate users through your application and pass their credentials to the SDK. This enables your application to make Copilot API requests on behalf of users who authorize your app. **How it works:** 1. User authorizes your OAuth GitHub App 1. Your app receives a user access token (`gho_` or `ghu_` prefix) -1. Pass the token to the SDK via `gitHubToken` option +1. Pass the token to the SDK through its client configuration **SDK Configuration:**
-Node.js / TypeScript +.NET -```typescript -import { CopilotClient } from "@github/copilot-sdk"; + +```csharp +using GitHub.Copilot; -const client = new CopilotClient({ - gitHubToken: userAccessToken, // Token from OAuth flow - useLoggedInUser: false, // Don't use stored CLI credentials +var userAccessToken = "token"; +await using CopilotClient client = new(new CopilotClientOptions +{ + GitHubToken = userAccessToken, + UseLoggedInUser = false, }); ``` + -
- -
-Python - -```python -from copilot import CopilotClient +```csharp +using GitHub.Copilot; -client = CopilotClient({ - "github_token": user_access_token, # Token from OAuth flow - "use_logged_in_user": False, # Don't use stored CLI credentials -}) -await client.start() +await using var client = new CopilotClient(new CopilotClientOptions +{ + GitHubToken = userAccessToken, // Token from OAuth flow + UseLoggedInUser = false, // Don't use stored CLI credentials +}); ```
@@ -168,41 +179,13 @@ func main() { import copilot "github.com/github/copilot-sdk/go" client := copilot.NewClient(&copilot.ClientOptions{ - GitHubToken: userAccessToken, // Token from OAuth flow - UseLoggedInUser: copilot.Bool(false), // Don't use stored CLI credentials + GitHubToken: userAccessToken, // Token from OAuth flow + UseLoggedInUser: copilot.Bool(false), // Don't use stored CLI credentials }) ``` -
-.NET - - -```csharp -using GitHub.Copilot; - -var userAccessToken = "token"; -await using var client = new CopilotClient(new CopilotClientOptions -{ - GitHubToken = userAccessToken, - UseLoggedInUser = false, -}); -``` - - -```csharp -using GitHub.Copilot; - -await using var client = new CopilotClient(new CopilotClientOptions -{ - GitHubToken = userAccessToken, // Token from OAuth flow - UseLoggedInUser = false, // Don't use stored CLI credentials -}); -``` - -
-
Java @@ -220,6 +203,50 @@ client.start().get();
+
+Python + +```python +from copilot import CopilotClient + +client = CopilotClient({ + "github_token": user_access_token, # Token from OAuth flow + "use_logged_in_user": False, # Don't use stored CLI credentials +}) +await client.start() +``` + +
+ +
+Rust + +```rust +use github_copilot_sdk::{Client, ClientOptions}; + +let client = Client::start( + ClientOptions::default() + .with_github_token(user_access_token) + .with_use_logged_in_user(false), +).await?; +``` + +
+ +
+TypeScript + +```typescript +import { CopilotClient } from "@github/copilot-sdk"; + +const client = new CopilotClient({ + gitHubToken: userAccessToken, // Token from OAuth flow + useLoggedInUser: false, // Don't use stored CLI credentials +}); +``` + +
+ **Supported token types:** * `gho_` - OAuth user access tokens * `ghu_` - GitHub App user access tokens @@ -233,6 +260,8 @@ client.start().get(); * SaaS applications building on top of Copilot * Any multi-user application where you need to make requests on behalf of different users +For more information, see [GitHub OAuth](../setup/github-oauth.md). + ## Environment variables For automation, CI/CD pipelines, and server-to-server scenarios, you can authenticate using environment variables. @@ -253,13 +282,52 @@ For organization-attributed automation that should not use a user's personal acc No code changes needed—the SDK automatically detects environment variables:
-Node.js / TypeScript +.NET -```typescript -import { CopilotClient } from "@github/copilot-sdk"; +```csharp +using GitHub.Copilot; // Token is read from environment variable automatically -const client = new CopilotClient(); +await using CopilotClient client = new(); +``` + +
+ +
+Go + + +```go +package main + +import copilot "github.com/github/copilot-sdk/go" + +func main() { + // Token is read from environment variable automatically + client := copilot.NewClient(nil) + _ = client +} +``` + + +```go +import copilot "github.com/github/copilot-sdk/go" + +// Token is read from environment variable automatically +client := copilot.NewClient(nil) +``` + +
+ +
+Java + +```java +import com.github.copilot.CopilotClient; + +// Token is read from environment variable automatically +var client = new CopilotClient(); +client.start().get(); ```
@@ -277,6 +345,30 @@ await client.start() +
+Rust + +```rust +use github_copilot_sdk::{Client, ClientOptions}; + +// Token is read from environment variable automatically +let client = Client::start(ClientOptions::default()).await?; +``` + +
+ +
+TypeScript + +```typescript +import { CopilotClient } from "@github/copilot-sdk"; + +// Token is read from environment variable automatically +const client = new CopilotClient(); +``` + +
+ **When to use:** * CI/CD pipelines (GitHub Actions, Jenkins, and more) * Automated testing @@ -313,40 +405,20 @@ For multi-user server mode, pass a per-session `gitHubToken` so each session run ## Disabling auto-login -To prevent the SDK from automatically using stored credentials or `gh` CLI auth, use the `useLoggedInUser: false` option: +To prevent the SDK from automatically using stored credentials or `gh` CLI auth, configure it to disable logged-in-user fallback:
-Node.js / TypeScript +.NET -```typescript -const client = new CopilotClient({ - useLoggedInUser: false, // Only use explicit tokens +```csharp +await using var client = new CopilotClient(new CopilotClientOptions +{ + UseLoggedInUser = false, // Only use explicit tokens }); ```
-
-Python - - -```python -from copilot import CopilotClient - -client = CopilotClient({ - "use_logged_in_user": False, -}) -``` - - -```python -client = CopilotClient({ - "use_logged_in_user": False, # Only use explicit tokens -}) -``` - -
-
Go @@ -373,18 +445,6 @@ client := copilot.NewClient(&copilot.ClientOptions{
-
-.NET - -```csharp -await using var client = new CopilotClient(new CopilotClientOptions -{ - UseLoggedInUser = false, // Only use explicit tokens -}); -``` - -
-
Java @@ -400,6 +460,51 @@ client.start().get();
+
+Python + + +```python +from copilot import CopilotClient + +client = CopilotClient({ + "use_logged_in_user": False, +}) +``` + + +```python +client = CopilotClient({ + "use_logged_in_user": False, # Only use explicit tokens +}) +``` + +
+ +
+Rust + +```rust +use github_copilot_sdk::{Client, ClientOptions}; + +let client = Client::start( + ClientOptions::default().with_use_logged_in_user(false), +).await?; +``` + +
+ +
+TypeScript + +```typescript +const client = new CopilotClient({ + useLoggedInUser: false, // Only use explicit tokens +}); +``` + +
+ ## Next steps * [BYOK Documentation](./byok.md) - Learn how to use your own API keys