Generator Public

Browse fretwik's Code

Viewing creations from @fretwik · Showing public creations only.

RapidAPI GPT Chat Completion

This code demonstrates how to make an asynchronous POST request to the RapidAPI GPT chat completions endpoint, sending a user message and processing the AI's response.

Code
Prompt: export default async function main(ctx) { const apiKey = process.env.RAPIDAPI_KEY; const url = "https://double-gpt.p.rapidapi.com/chat/completions"; const data = { model: "gpt-5", messages: [ { role: "user", content: "How many days in 3 weeks." } ] }; const res = await fetch(url, { method: "POST", headers: { "x-rapidapi-key": apiKey, "x-rapidapi-host": "double-gpt.p.rapidapi.com", "Content-Type": "application/json" }, body: JSON.stringify(data) }); if (!res.ok) { throw new Error(`Erro: ${res.status} - ${await res.text()}`); } const json = await res.json(); const answer = json.choices?.[0]?.message?.content ?? null; console.log("Resposta bruta:", JSON.stringify(json, null, 2)); console.log("Só o texto:", answer); return { answer }; }