SwiftRelay

← All sprint notes

First Run: A Node.js Script That Talks to a Model

First lapsEN

Your first language model integration can be a small terminal program: send a question, receive an answer, and print it. This walkthrough uses Node.js and the OpenAI JavaScript SDK with SwiftRelay. You will finish with a working script and a clear place to change its prompt and model.

1. Prepare Node.js and an empty project

Install Node.js 22 or later, then open a terminal. Run the following command to check your installation. This walkthrough uses Node’s built-in support for loading environment variables from a file.

node --version

Create a folder named first-model-run and move into it. Run these commands separately to create a package file and install the SDK:

npm init -y

npm install openai

The SDK handles request formatting and response parsing. Its client also accepts a custom API address, which lets this same package send requests through SwiftRelay.

2. Get your SwiftRelay key and model identifier

Sign up for SwiftRelay, create an API key in your account, and copy it somewhere private. New accounts receive a small gift credit, which you can use for an initial experiment. SwiftRelay bills per token, so both the text you send and the text the model generates contribute to usage.

Choose an available model, such as Gemini 2.5 Flash, and copy its exact API identifier from SwiftRelay’s model listing. A model’s display name and request identifier can differ, so use the listed identifier in your configuration.

For the OpenAI SDK, set the base URL to https://api.fastapirelay.com/v1. The /v1 suffix belongs in this configuration; the bare host is used by other integrations, including Claude Code and Gemini CLI.

3. Store your credentials in a local environment file

Create a file named .env in your project folder. Add the following three lines, replacing the key and model placeholders with your own values:

SWIFTRELAY_API_KEY=your_api_key_here

SWIFTRELAY_BASE_URL=https://api.fastapirelay.com/v1

SWIFTRELAY_MODEL=your_exact_model_identifier

Add .env to your .gitignore file before committing the project. Keep API keys out of source files, screenshots, and shared terminal output. If a key becomes public, revoke it and create a replacement.

4. Write the request in first-run.mjs

Create first-run.mjs beside your .env file. The .mjs extension enables JavaScript module syntax, so you can import the SDK without changing package settings. Copy the following JavaScript into the file; it is valid as a single line.

import OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.SWIFTRELAY_API_KEY, baseURL: process.env.SWIFTRELAY_BASE_URL }); async function main() { if (!process.env.SWIFTRELAY_MODEL) throw new Error('Set SWIFTRELAY_MODEL in .env'); const result = await client.chat.completions.create({ model: process.env.SWIFTRELAY_MODEL, messages: [{ role: 'user', content: 'Explain what a Node.js module is in three short sentences.' }] }); const answer = result.choices[0]?.message?.content; if (!answer) throw new Error('The response contained no text'); console.log(answer); if (result.usage) console.log('Token usage:', result.usage); } main().catch(error => { console.error('Request failed:', error.message); process.exitCode = 1; });

The messages array contains the conversation you send. Here it holds one user message. The model field selects the configured model, and the first choice contains the returned answer. Usage information, when provided, helps you see the tokens associated with the request.

5. Run the script and check the response

From the project folder, run:

node --env-file=.env first-run.mjs

You should see an explanation of Node.js modules, followed by token usage if the response includes it. The wording can vary between runs. Check that the answer addresses your prompt instead of expecting an exact sentence.

If the request fails, use the error message to guide your next check:

  • Authentication error: Check that your API key was copied correctly and remains active.
  • Model error: Verify the exact model identifier and its availability in your account.
  • Connection error: Check your network connection and confirm that the base URL ends in /v1.

6. Change one prompt and inspect the result

Replace the prompt with a task you can judge easily, such as asking for three names for a gardening app. Run the script again and inspect the answer and token usage. Keeping each experiment small makes it easier to understand the API before adding conversation history or connecting the script to an application.

Keep reading

Back for another lap?Log in