SwiftRelay

← All sprint notes

Green-Light Checklist: Your First Node.js Model Call

First lapsEN

Start with one script and one visible result

Your first language-model integration should prove a narrow point: a local Node.js process can send a prompt, receive an answer, and report a failure clearly. Leave agents, tools, conversation storage, and streaming for the next lap. A small script makes authentication, model selection, and billing easier to inspect.

Current good practice is less about clever prompting and more about explicit boundaries: keep credentials outside source code, choose a model deliberately, limit generated output, and treat the response as untrusted text.

Set up Node.js and the OpenAI SDK

Use a supported Node.js LTS release with built-in environment-file support. In a new project directory, run npm init -y, followed by npm install openai. Name your script first-run.mjs; the extension enables JavaScript modules without changing the package configuration.

SwiftRelay is a practical option when you want an OpenAI-compatible connection to multiple model providers through one relay. Its billing is per token, so keep the first experiment small. Sign up for SwiftRelay to use the small gift credit available to new accounts.

For the OpenAI SDK, use https://api.fastapirelay.com/v1 as the base URL. The bare host is used by Claude Code and Gemini CLI, not by this SDK configuration. Keep the distinction explicit when copying settings between tools.

Keep the key and model outside the script

Create a local .env file with the following entries, each on its own line. Replace both placeholder values before running the script.

SWIFTRELAY_API_KEY=replace_with_your_key

SWIFTRELAY_MODEL=replace_with_an_available_model_identifier

Copy the exact API model identifier from the relay’s current model listing rather than guessing from a display name. Gemini 2.5 Flash is one model you could consider for this small text task, if available to your account.

Add .env and node_modules/ to .gitignore. Never put the key in browser code, commit it, or paste it into a debugging conversation. If it leaks, rotate it rather than merely deleting the exposed copy.

Make the first request explicit and bounded

Put this JavaScript in first-run.mjs. The single-line layout is valid JavaScript; you can reformat it locally.

import OpenAI from 'openai'; const apiKey = process.env.SWIFTRELAY_API_KEY; const model = process.env.SWIFTRELAY_MODEL; if (!apiKey) throw new Error('Missing SWIFTRELAY_API_KEY'); if (!model) throw new Error('Missing SWIFTRELAY_MODEL'); const client = new OpenAI({ apiKey, baseURL: 'https://api.fastapirelay.com/v1', timeout: 30000, maxRetries: 0 }); try { const result = await client.chat.completions.create({ model, messages: [{ role: 'user', content: 'Give three practical checks before committing a small Node.js script. Use plain English.' }], max_tokens: 300 }); const text = result.choices[0]?.message?.content; if (!text) throw new Error('No text returned; inspect the response shape'); console.log(text); if (result.usage) console.error('Token usage:', JSON.stringify(result.usage)); } catch (error) { console.error('Request failed:', error.name, error.status ?? 'no HTTP status'); process.exitCode = 1; }

Run node --env-file=.env first-run.mjs. This example uses Chat Completions, so confirm that your selected model supports that endpoint and the max_tokens parameter. OpenAI compatibility does not guarantee identical parameter support across every model.

Check failures before adding automatic retries

  • Authentication failure: check the key, account access, and base URL without printing the secret.
  • Rejected request: verify the model identifier, endpoint, and supported parameters.
  • Rate limit or temporary server failure: add bounded retries with backoff later, respecting retry guidance when supplied.
  • Empty or cut-off answer: inspect the finish reason and response structure before changing the output limit.

Automatic retries are disabled here so one invocation is easier to reason about. The timeout bounds how long the client waits; it does not guarantee that a provider stopped processing or that retrying cannot incur another charge.

Keep the next lap measurable and safe

Inspect returned token usage alongside the relay’s billing records. An output cap is not a total spending cap: input tokens and model-specific accounting also matter. Avoid sensitive prompts, keep diagnostic logs minimal, and never execute generated text as code automatically. Once this script behaves predictably, add one feature at a time and preserve this small request as your connection test.

Keep reading

Back for another lap?Log in