DocumentationOpenAI API

OpenAI API

Call AI models securely from your frontend.

Call OpenAI's completion API without leaking your API key to the browser.

api/fuseplane/chat.ts
1import { FusePlaneEndpoint } from '@fuseplane/core';
2
3export default FusePlaneEndpoint({
4 async run({ secrets, body }) {
5 const prompt = body.prompt;
6
7 const response = await fetch('https://api.openai.com/v1/chat/completions', {
8 method: 'POST',
9 headers: {
10 'Content-Type': 'application/json',
11 'Authorization': `Bearer ${secrets.OPENAI_API_KEY}`
12 },
13 body: JSON.stringify({
14 model: "gpt-4",
15 messages: [{ role: "user", content: prompt }]
16 })
17 });
18
19 const data = await response.json();
20 return {
21 reply: data.choices[0].message.content
22 };
23 }
24});