Skip to content

openai/openai-quickstart-node

Repository files navigation

OpenAI API Quickstart - Node.js

This repository provides a collection of examples demonstrating how to use the OpenAI APIs with the Node.js SDK.

The examples are organized by API, with each folder dedicated to a specific API:

  • Chat Completions
  • Assistants
  • Fine-tuning
  • Embeddings
  • Moderation
  • Batch Processing
  • Images

Within each folder, you'll find a basic example to get started. For some APIs, additional examples are also included to explore more advanced use cases.

Prerequisites

To run the examples with the Node.js SDK, you will need:

  • A recent version of Node.js (>= 16.0.0)
  • A recent version of npm or another node package manager
  • An OpenAI API key (you can get one from your OpenAI dasard)

How to use

  1. Clone this repository

    $ git clone https://.com/openai/openai-quickstart-node.git
  2. Navigate into the project directory

    $ cd openai-quickstart-node
  3. Install the OpenAI Node.js SDK

    $ npm install openai
  4. Set your OpenAI API key in environment variables

    In the terminal session:

    Bash (Mac/Linux):

    $ export OPENAI_API_KEY=<your-api-key>

    PowerShell (Windows):

    $ setx OPENAI_API_KEY "<your_api_key>"

    Set it globally:

    Add this line to your .bashrc or .zshrc file on Mac/Linux:

    $ export OPENAI_API_KEY=<your-api-key>

    Or update your system environment variables on Windows.

  5. Run each script individually

    $ node path/to/script.js

    For example, to run the basic chat completions example:

    $ node chat_completions/index.js

Examples

Explore the examples below to learn how to use the Node.js SDK for your specific use case.

APIExamplePath
Chat CompletionsBasic examplechat_completions/index.js
Multi-turn conversationchat_completions/multi_turn.js
Function Callingchat_completions/function_calling.js
Vision (image input)chat_completions/vision.js
AssistantsCreate an assistantassistants/index.js
Example threadassistants/thread.js {assistant-id}
Fine-tuningCreate a fine-tuned modelfine_tuning/index.js
Use a fine-tuned modelfine_tuning/use_model.js {job-id}
EmbeddingsGenerate embeddingsembeddings/index.js
ModerationModerate textmoderation/index.js
Moderate images & textmoderation/images.js
BatchCreate a batch jobbatch/index.js
Get batch job resultsbatch/retrieve_results.js {job-id}
ImagesGenerate an imageimages/index.js

Additional Resources

For more in-depth examples within front-end applications, including with responses, check out these additional resources: