fastify-openai

OpenAI Node.js Library instance initialization and encapsulation for the Fastify framework.
Install
Install the package:
npm i fastify-openai
yarn add fastify-openai
pnpm add fastify-openai
bun add fastify-openai
Usage
The package needs to be added to your project with register and you must at least configure your account's secret key wich is available in your OpenAI Dashboard then call the OpenAI API and you are done.
Importing the package
import fastifyOpenAI from "fastify-openai";
const fastifyOpenAI = require("fastify-openai");
JavaScript + CJS
const fastify = require("fastify")({ logger: true });
fastify.register(require("fastify-openai"), {
apiKey: "sk-TacO...",
});
fastify.post("/chat", async (request, reply) => {
const chat = await fastify.openai.chat.completions.create({
messages: [{ role: "user", content: "Hello, Fastify!" }],
model: "gpt-4o-mini",
});
return chat;
});
fastify.listen({ port: 3000 });
TypeScript + ESM
import Fastify from "fastify";
import fastifyOpenAI from "fastify-openai";
const fastify = Fastify({ logger: true });
fastify.register(fastifyOpenAI, {
apiKey: "sk-TacO...",
});
fastify.post("/chat", async (request, reply) => {
const chat = await fastify.openai.chat.completions.create({
messages: [{ role: "user", content: "Hello, Fastify!" }],
model: "gpt-4o-mini",
});
return chat;
});
fastify.listen({ port: 3000 });
declare module "fastify" {
interface FastifyInstance {
openai: FastifyOpenAI;
}
}
Options
-
apiKey [ required ]: Your account's secret key wich is available in your OpenAI Dashboard
-
name [ optional ]: Through this option fastify-openai lets you define multiple OpenAI instances, with different configurations.
-
organization [ optional ]: The organization ID to use for this request. If not provided, the request will be made with the default organization.
-
project [ optional ]: The project ID to use for this request. If not provided, the request will be made with the default project.
-
baseURL [ optional ]: The base URL for the API. Defaults to https://api.openai.com/v1.
-
timeout [ optional ]: The request timeout in milliseconds.
-
httpAgent [ optional ]: An HTTP agent used to manage HTTP(S) connections.
-
maxRetries [ optional ]: The maximum number of retries for a request.
Multiple plugin instances (TypeScript + ESM)
When using multiple plugin instances, the name property is required for each instance.
import Fastify from "fastify";
import fastifyOpenAI from "fastify-openai";
const fastify = Fastify({ logger: true });
fastify
.register(require("fastify-openai"), {
apiKey: "sk-Te5t...",
name: "test",
timeout: 28000,
})
.register(require("fastify-openai"), {
apiKey: "sk-Pr0d...",
name: "prod",
});
fastify.post("/test/chat", function (request, reply) {
const testChat = await fastify.openai.test.chat.completions.create({
messages: [{ role: "user", content: "Hello, Test!" }],
model: "gpt-4o-mini",
});
return testChat;
});
fastify.post("/prod/chat", function (request, reply) {
const prodChat = await fastify.openai.prod.chat.completions.create({
messages: [{ role: "user", content: "Hello, Production!" }],
model: "gpt-4o-mini",
});
return prodChat;
});
fastify.listen({ port: 3000 });
declare module "fastify" {
interface FastifyInstance {
openai: {
test: FastifyOpenAI;
prod: FastifyOpenAI;
}
}
}
Documentation
See the Node OpenAI API docs.
Acknowledgements
This project is kindly sponsored by @timmywheels.
License
Licensed under MIT