Skip to main content
You can integrate Portrix into JavaScript and TypeScript projects using the official openai npm package, the native Fetch API for lightweight scripts, or Axios for projects that already depend on it. All three approaches share the same base URL and Bearer token authentication pattern, and every method is fully compatible with TypeScript’s type system.

Installation

Basic usage

Create an OpenAI client configured for Portrix and make a typed chat completion request:

Environment variables

Read your Portrix API key from process.env so it never appears in source code. In Node.js projects, load a .env file with a package like dotenv:
Never hardcode your API key in source files or commit it to version control. Treat it like a password: store it in environment variables, secrets managers, or your platform’s secure configuration system.

Browser usage

Do not call Portrix directly from browser-side JavaScript. Doing so exposes your API key to anyone who inspects your page’s network traffic. Instead, create a server-side proxy endpoint (e.g. a Next.js API route or an Express handler) that holds the key and forwards requests on behalf of the client.
See the Next.js integration section below for a complete server-side proxy example.

Using the Fetch API

For environments without the SDK — such as Cloudflare Workers or lightweight scripts — use the Fetch API directly:

Streaming in JavaScript

Use stream: true with the openai SDK and async iteration to print tokens as they arrive:

Error handling

Catch typed errors from the openai package to handle rate limits, invalid requests, and network issues:

Next.js integration

Create a server-side API route in Next.js to proxy Portrix requests. Your API key stays on the server and is never exposed to the browser:
Your client-side code then calls /api/chat — it never touches Portrix directly and never sees the API key.
In Next.js, prefix environment variables with NEXT_PUBLIC_ only when they must be readable in the browser. Keep PORTRIX_API_KEY unprefixed so Next.js automatically restricts it to server-side runtimes.