Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

fastify-openai

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-openai

OpenAI Fastify plugin

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
2.5K
-20.25%
Maintainers
1
Weekly downloads
 
Created
Source

fastify-openai

NPM version GitHub CI Coverage Status js-standard-style

OpenAI Node.js Library instance initialization and encapsulation for the Fastify framework.

Install

Install the package:

# npm
npm i fastify-openai

# yarn
yarn add fastify-openai

# pnpm
pnpm add fastify-openai

# bun
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

// ESM
import fastifyOpenAI from "fastify-openai";

// CJS
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) => {
  // create a chat completion using the OpenAI API
  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) => {
  // create a chat completion using the OpenAI API
  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 declaration merging
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, // in ms (this is 28 seconds)
  })
  .register(require("fastify-openai"), {
    apiKey: "sk-Pr0d...",
    name: "prod",
  });

fastify.post("/test/chat", function (request, reply) {
  // create a chat completion using the OpenAI API 'test' instance
  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) {
  // create a chat completion using the OpenAI API 'prod' instance
  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 });

// typescript declaration merging
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

Keywords

openai

FAQs

Package last updated on 26 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts