Skip to content

Commit 8aa71bb

Browse files
authored
docs: #463 improve Realtime Agent getting started guide document (#464)
1 parent 1bef1fa commit 8aa71bb

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

docs/src/content/docs/guides/voice-agents/quickstart.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import thinClientExample from '../../../../../../examples/docs/voice-agents/thin
3131
In this quickstart we will create a voice agent you can use in the browser. If you want to check out a new project, you can try out [`Next.js`](https://nextjs.org/docs/getting-started/installation) or [`Vite`](https://vite.dev/guide/installation.html).
3232

3333
```bash
34-
npm create vite@latest my-project --template vanilla-ts
34+
npm create vite@latest my-project -- --template vanilla-ts
3535
```
3636

3737
1. **Install the Agents SDK**
@@ -47,6 +47,7 @@ import thinClientExample from '../../../../../../examples/docs/voice-agents/thin
4747
As this application will run in the user's browser, we need a secure way to connect to the model through the Realtime API. For this we can use an [ephemeral client key](https://platform.openai.com/docs/guides/realtime#creating-an-ephemeral-token) that should be generated on your backend server. For testing purposes you can also generate a key using `curl` and your regular OpenAI API key.
4848

4949
```bash
50+
export OPENAI_API_KEY="sk-proj-...(your own key here)"
5051
curl -X POST https://api.openai.com/v1/realtime/client_secrets \
5152
-H "Authorization: Bearer $OPENAI_API_KEY" \
5253
-H "Content-Type: application/json" \
@@ -58,7 +59,7 @@ import thinClientExample from '../../../../../../examples/docs/voice-agents/thin
5859
}'
5960
```
6061

61-
The response will contain a `client_secret.value` value that you can use to connect later on. Note that this key is only valid for a short period of time and will need to be regenerated.
62+
The response will contain a "value" string a the top level, which starts with "ek\_" prefix. You can use this ephemeral key to establish a WebRTC connection later on. Note that this key is only valid for a short period of time and will need to be regenerated.
6263

6364
3. **Create your first Agent**
6465

@@ -92,7 +93,7 @@ import thinClientExample from '../../../../../../examples/docs/voice-agents/thin
9293
To connect to the session you need to pass the client ephemeral token you generated earlier on.
9394

9495
```typescript
95-
await session.connect({ apiKey: '<client-api-key>' });
96+
await session.connect({ apiKey: 'ek_...(put your own key here)' });
9697
```
9798

9899
This will connect to the Realtime API using WebRTC in the browser and automatically configure your microphone and speaker for audio input and output. If you are running your `RealtimeSession` on a backend server (like Node.js) the SDK will automatically use WebSocket as a connection. You can learn more about the different transport layers in the [Realtime Transport Layer](/openai-agents-js/guides/voice-agents/transport) guide.
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { RealtimeAgent, RealtimeSession } from '@openai/agents/realtime';
22

3-
const agent = new RealtimeAgent({
4-
name: 'Assistant',
5-
instructions: 'You are a helpful assistant.',
6-
});
3+
export async function setupCounter(element: HTMLButtonElement) {
4+
// ....
5+
// for quickly start, you can append the following code to the auto-generated TS code
76

8-
const session = new RealtimeSession(agent);
9-
10-
// Automatically connects your microphone and audio output
11-
// in the browser via WebRTC.
12-
await session.connect({
13-
apiKey: '<client-api-key>',
14-
});
7+
const agent = new RealtimeAgent({
8+
name: 'Assistant',
9+
instructions: 'You are a helpful assistant.',
10+
});
11+
const session = new RealtimeSession(agent);
12+
// Automatically connects your microphone and audio output in the browser via WebRTC.
13+
try {
14+
await session.connect({
15+
// To get this ephemeral key string, you can run the following command or implement the equivalent on the server side:
16+
// curl -s -X POST https://api.openai.com/v1/realtime/client_secrets -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"session": {"type": "realtime", "model": "gpt-realtime"}}' | jq .value
17+
apiKey: 'ek_...(put your own key here)',
18+
});
19+
console.log('You are connected!');
20+
} catch (e) {
21+
console.error(e);
22+
}
23+
}

0 commit comments

Comments
 (0)