MyRelay

← Volver al cuaderno

Use the OpenAI Node.js SDK with a Custom Endpoint

Start with one small, measurable request

Connecting an SDK to a relay should begin with a modest test, not your entire application. This walkthrough takes you from preparing a Node.js project to checking a completed request and its cost. Keep the first prompt short, avoid private data, and decide how much you are comfortable spending before you experiment.

Prepare your project and protect your key

First, add the official openai package to your Node.js project using your usual package manager. Import its OpenAI client in your server-side application. Keep the integration on the server rather than enabling browser access, which risks exposing your secret API credentials. Store credentials outside your source code and exclude them from screenshots and shared logs. ([github.com](https://github.com/openai/openai-node))

Create a MyRelay account, choose a small starting budget, and generate an API key in the dashboard. Use that relay key for this connection rather than a key issued directly by another provider. MyRelay provides the address and key instructions together, so keep those instructions available while setting up the client. ([myapirelay.com](https://myapirelay.com/how-to-connect?hl=en))

Give the client the correct base address

Next, construct the OpenAI client with two explicit options: apiKey and baseURL. The first supplies your MyRelay credential; the second changes where the SDK sends requests. The SDK also supports OPENAI_API_KEY and OPENAI_BASE_URL as environment defaults, but explicit client options can make an initial integration easier to inspect. Choose one approach deliberately rather than leaving competing settings unexplained. ([github.com](https://github.com/openai/openai-node/blob/main/docs/configuration.md?utm_source=openai))

Copy the OpenAI-compatible address from MyRelay’s connection instructions, including its version-one suffix. Do not substitute the website address or paste the complete chat-completion request address into baseURL. MyRelay distinguishes the SDK’s base address from the chat endpoint; copying the correct one avoids an unnecessary troubleshooting loop. ([myapirelay.com](https://myapirelay.com/how-to-connect?hl=en))

Select a supported model and request method

For this walkthrough, use GPT-6.1 Sol and copy its exact API identifier from MyRelay’s instructions. A display name is not necessarily the identifier accepted in a request. Start with the SDK’s chat.completions.create method, which matches the Chat Completions route documented by MyRelay. ([myapirelay.com](https://myapirelay.com/how-to-connect?hl=en))

Send a messages collection containing one user message asking for a two-sentence explanation of a familiar concept. Await the result, then read the first choice’s message content. Keep streaming, tools, images, and conversation history out of this first test. OpenAI-compatible routing should not be treated as a promise that every API feature works identically across providers. Verify additional features separately before depending on them. ([github.com](https://github.com/openai/openai-node))

Check the result before increasing usage

Run the request once and confirm that the returned text answers your prompt. Then check the corresponding request in MyRelay’s usage view, where you can inspect tokens and cost. Record the model, approximate prompt length, and charge in your testing notes. MyRelay charges for the tokens actually used, including applicable cache reads and writes. ([myapirelay.com](https://myapirelay.com/how-to-connect?hl=en))

Make your next test resemble a real task, but keep the sample small. Compare a short answer with a longer requested explanation before adding background material. Treat the observed charge as a measurement of that request, not a guaranteed price for every future interaction.

Set retry boundaries before building a loop

Before running repeated tests, review maxRetries and timeout. The SDK retries certain temporary failures twice by default, and timed-out requests can also be retried. Consider disabling automatic retries during initial debugging so that repeated attempts are easier to track. A timeout is a waiting limit, not a spending cap or proof that remote processing stopped. ([github.com](https://github.com/openai/openai-node/blob/main/docs/configuration.md?utm_source=openai))

If a request fails, inspect its status and message before sending it again. Check credentials, the base address, and the model identifier first. MyRelay documents HTTP 402 as an exhausted budget, so repeated retries will not resolve it. Once one request works and its cost is understood, introduce conversation history or other features one at a time. ([myapirelay.com](https://myapirelay.com/how-to-connect?hl=en))

Más para leer