forked from openai/openai-realtime-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (45 loc) · 1.29 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Fastify from "fastify";
import FastifyVite from "@fastify/vite";
import dotenv from 'dotenv';
dotenv.config();
// Fastify + React + Vite configuration
const server = Fastify({
logger: {
transport: {
target: "@fastify/one-line-logger",
},
},
});
// Check for the OpenAI API key
if (!process.env.OPENAI_API_KEY) {
console.error("Error: OPENAI_API_KEY is not defined in the environment variables.");
process.exit(1); // Exit the process if the API key is not found
} else {
console.log("API key found. Proceeding with server setup...");
}
await server.register(FastifyVite, {
root: import.meta.url,
renderer: "@fastify/react",
});
await server.vite.ready();
// Server-side API route to return an ephemeral realtime session token
server.get("/token", async () => {
const r = await fetch("https://api.openai.com/v1/realtime/sessions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-4o-mini-realtime-preview-2024-12-17",
voice: "verse",
}),
});
return new Response(r.body, {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
});
await server.listen({ port: process.env.PORT || 3000 });