2 minutes reading time

On this page


OpenAI with Node.js - Integrations

2 minutes reading time

OpenAI Node.js
Integrations

OpenAI with Node.js

Learn how to configure OpenAI with Dotenv Vault in a simple Node.js web app. This tutorial assumes you are already familiar with .env files and know how to sync them.

You can find a complete example repo here.

Initial setup

First, you will need an OpenAI setup, so if you don’t have one already go ahead and create it from scratch or one of the platform’s templates.

Package installation

Once ready, proceed by installing the dotenv package with npm.

CLI
npm install dotenv --save

Reference the Vault package as early in your index.js code as possible to skip any conflicts that may arise.

Node.js
// index.js
require('dotenv').config()
console.log(process.env) // for debugging purposes. remove when ready.

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
console.log(process.env.TEST);
console.log(openai);

Example.

Build the Vault

Confirm you are logged in and your Vault is synced locally by running npx dotenv-vault pull development. Once ready, proceed by building your Vault with npx dotenv-vault build.

CLI
npx dotenv-vault build

Once Vault has finished building, it will provide you with access to its decryption keys, which you can use to interact with protected environment variables with ease.

To retrieve a key, just input npx dotenv-vault keys, followed by your preferred environment, like development, for example. You can do the same with other environments such as ci and production.

The outcome of this will be a long URL being returned. You will immediately recognize it as it always starts with dotenv://:key and ends in ?environment= with the environment you have chosen.

CLI
npx dotenv-vault keys development
remote:   Listing .env.vault decryption keys... done

dotenv://:[email protected]/vault/.env.vault?environment=development

Set deployment

With the decryption key safely in your possession, it is time for you to set it as an environment variable on your client machine via CLI. Enter the key-value pair directly for UNIX systems or preceded by set for Windows. The set environment variable will remain available until you exit the CLI.

CLI
// UNIX
DOTENV_KEY=dotenv://:[email protected]/vault/.env.vault?environment=development

// Windows
set DOTENV_KEY=dotenv://:[email protected]/vault/.env.vault?environment=development

Commit and push

That’s it!

Commit those changes safely to code and deploy your OpenAI project.

When the build runs, it will recognize the DOTENV_KEY, decrypt the .env.vault file, and load the development environment variables to ENV.

If a DOTENV_KEY is not set when developing on local machine, for example, it will fall back to standard Dotenv functionality.

You’ll know things worked correctly when you see the OpenAI environment context printed in your CLI.