Skip to content

Node.JS API

This page describes how to import and use the cli as an API in your Node.JS application.

Assuming you have have added the cli as a dependency in your project, you can import the cli as follows:

Terminal window
npm i -D genaiscript

The API can be imported using imports from “genaiscript/api”.

import { run } from "genaiscript/api"

The imported api.mjs wrapper is a tiny, zero dependency loader that spawns a Node.JS worker thread to run GenAIScript.

  • No pollutation of the globals
  • No side effects on the process

run

The run function wraps the cli run command.

import { run } from "genaiscript/api"
const results = await run("summarize", ["myfile.txt"])

Environment variables

You can set the environment variables for the GenAIScript process by passing an object as the env field in the options. By default, the worker will inherit process.env.

const results = await run("summarize", ["myfile.txt"], {
env: {
MY_ENV_VAR: "value",
},
})