Skip to content

Commit 4878f14

Browse files
sunbryejc-clark
andauthored
Copilot SDK - Quickstart (#59593)
Co-authored-by: Joe Clark <31087804+jc-clark@users.noreply.github.com>
1 parent 19552e0 commit 4878f14

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: GitHub Copilot SDK
3+
shortTitle: Copilot SDK
4+
intro: Learn how to customize your {% data variables.product.prodname_copilot_short %} experience using {% data variables.copilot.copilot_sdk_short %}.
5+
versions:
6+
feature: copilot
7+
children:
8+
- /sdk-getting-started
9+
contentType: how-tos
10+
---
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: Getting started with Copilot SDK
3+
shortTitle: Quickstart
4+
intro: 'Learn how to install {% data variables.copilot.copilot_sdk_short %} and send your first message.'
5+
allowTitleToDifferFromFilename: true
6+
product: '{% data reusables.gated-features.copilot-sdk %}'
7+
versions:
8+
feature: copilot
9+
topics:
10+
- Copilot
11+
contentType: get-started
12+
category:
13+
- Learn about Copilot
14+
- Author and optimize with Copilot
15+
---
16+
17+
> [!NOTE]
18+
>{% data variables.copilot.copilot_sdk_short %} is currently in {% data variables.release-phases.technical_preview %}. Functionality and availability are subject to change.
19+
20+
{% data variables.copilot.copilot_sdk %} lets you build applications powered by {% data variables.product.prodname_copilot %} in your preferred programming language. In this guide, you'll install the SDK using `npm`, send your first message, and add streaming responses.
21+
22+
For more information and steps for other languages, see [Install the SDK](https://github.com/github/copilot-sdk/blob/main/docs/getting-started.md) in the `github/copilot-sdk` repository.
23+
24+
## Prerequisites
25+
26+
Before you begin, make sure you have **Node.js 18** or later installed.
27+
28+
## Authentication
29+
30+
Follow the instructions at [AUTOTITLE](/copilot/how-tos/copilot-cli/install-copilot-cli) to install and authenticate with {% data variables.copilot.copilot_cli %}. This will allow the SDK to access your {% data variables.product.github %} account and use {% data variables.product.prodname_copilot_short %}.
31+
32+
1. Verify that {% data variables.copilot.copilot_cli_short %} is installed and working:
33+
34+
```bash copy
35+
copilot --version
36+
```
37+
38+
## Installation
39+
40+
1. Create a new directory and initialize your project:
41+
42+
```bash copy
43+
mkdir copilot-demo && cd copilot-demo
44+
npm init -y --init-type module
45+
```
46+
47+
1. Install the SDK and TypeScript runner:
48+
49+
```bash copy
50+
npm install @github/copilot-sdk tsx
51+
```
52+
53+
## Send your first message
54+
55+
1. Create a new file `index.ts` and add the following code. This sends a single prompt to {% data variables.product.prodname_copilot_short %} and prints the response.
56+
57+
```typescript copy
58+
import { CopilotClient } from "@github/copilot-sdk";
59+
60+
const client = new CopilotClient();
61+
const session = await client.createSession({ model: "gpt-4.1" });
62+
63+
const response = await session.sendAndWait({ prompt: "What is 2 + 2?" });
64+
console.log(response?.data.content);
65+
66+
await client.stop();
67+
process.exit(0);
68+
```
69+
70+
1. Run the code:
71+
72+
```bash copy
73+
npx tsx index.ts
74+
```
75+
76+
In this example:
77+
78+
* **`CopilotClient()`** creates a new client that manages the connection to {% data variables.copilot.copilot_cli_short %}.
79+
* **`createSession()`** starts a new conversation session with the specified model.
80+
* **`sendAndWait()`** sends a prompt and waits for the complete response before returning.
81+
82+
## Add streaming responses
83+
84+
Instead of waiting for the full response, you can stream it as it's generated. This is useful for long responses or interactive applications where you want to display output in real time.
85+
86+
1. Update `index.ts` with the following code to listen and print response chunks as they arrive:
87+
88+
```typescript copy
89+
import { CopilotClient } from "@github/copilot-sdk";
90+
91+
const client = new CopilotClient();
92+
const session = await client.createSession({
93+
model: "gpt-4.1",
94+
streaming: true,
95+
});
96+
97+
// Listen for response chunks
98+
session.on("assistant.message_delta", (event) => {
99+
process.stdout.write(event.data.deltaContent);
100+
});
101+
session.on("session.idle", () => {
102+
console.log(); // New line when done
103+
});
104+
105+
await session.sendAndWait({ prompt: "Tell me a short joke" });
106+
107+
await client.stop();
108+
process.exit(0);
109+
```
110+
111+
1. Run the code:
112+
113+
```bash copy
114+
npx tsx index.ts
115+
```
116+
117+
With streaming enabled, the response appears incrementally as it's generated.
118+
You can subscribe to events to process each chunk in real time:
119+
120+
* **`assistant.message_delta`** fires for each chunk of the response as it's generated.
121+
* **`session.idle`** fires when the response is complete and the session is ready for the next message.
122+
123+
### Event subscription methods
124+
125+
The SDK provides the following methods to subscribe to events:
126+
127+
* **on(handler)**: Subscribe to all events. Returns the unsubscribe function.
128+
* **on(eventType, handler)**: Subscribe to a specific event type. Returns the unsubscribe function.
129+
130+
Add the following code to `index.ts` to subscribe to events and unsubscribe when you no longer need them:
131+
132+
```typescript copy
133+
// Subscribe to all events
134+
const unsubscribeAll = session.on((event) => {
135+
console.log("Event:", event.type);
136+
});
137+
138+
// Subscribe to specific event type
139+
const unsubscribeIdle = session.on("session.idle", (event) => {
140+
console.log("Session is idle");
141+
});
142+
143+
// Later, to unsubscribe:
144+
unsubscribeAll();
145+
unsubscribeIdle();
146+
```
147+
148+
## Next steps
149+
150+
To continue getting started with {% data variables.copilot.copilot_sdk_short %}, see [Build Your First Copilot-Powered App](https://github.com/github/copilot-sdk/blob/main/docs/getting-started.md#step-4-add-a-custom-tool) in the `github/copilot-sdk` repository.

content/copilot/how-tos/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ children:
1111
- /get-code-suggestions
1212
- /chat-with-copilot
1313
- /copilot-cli
14+
- /copilot-sdk
1415
- /use-copilot-agents
1516
- /use-ai-models
1617
- /provide-context
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% data variables.copilot.copilot_sdk %} is available with all {% data variables.product.prodname_copilot_short %} plans.

data/variables/copilot.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ copilot_custom_agent_caps_short: 'Custom agent'
8282
custom_agents_short: 'custom agents'
8383
custom_agents_caps_short: 'Custom agents'
8484

85+
## Copilot SDK
86+
copilot_sdk: 'GitHub Copilot SDK'
87+
copilot_sdk_short: 'Copilot SDK'
88+
8589
## Copilot subagents
8690
subagent_short: 'subagent'
8791
subagent_caps_short: 'Subagent'

0 commit comments

Comments
 (0)