Node.js + Express: Building a Safe AI Proxy Endpoint

By Darus S Alfatah · 2025-09-07 · Tutorial · 7 menit

Node.js + Express: Building a Safe AI Proxy Endpoint

Author note: Never send the key to the browser. In Express, all AI calls go through the server.

Focus of this article

This article is Node.js + Express specific: routes, streaming, and error handling. For other web frameworks, see the related articles.

Read also: Integrating an AI API in Laravel · AI Routes in Next.js: Server Handlers and Streaming

Why a Proxy

Calling the model directly from the browser exposes the key to anyone who opens developer tools. A small server endpoint keeps the key private, validates requests, and gives you one place to log usage and enforce limits.

Step-by-Step Build

  1. Create an Express app with JSON body parsing and a size limit.
  2. Add session or token authentication for your users.
  3. Validate message count and text length.
  4. Call the model with a server-side key.
  5. Return only the fields the client needs.
  6. Log tokens, latency, and status per request.
  7. Add a rate limiter per user.
  8. Set timeouts so slow requests do not pile up.

Input Validation

Reject payloads that are too large or misshapen before they reach the provider. Cheap validation prevents expensive mistakes: a single oversized request can consume a day of budget when the key is shared.

Error Translation

Never forward raw provider errors. Map them to a small set of messages your client understands, and log the details server-side. Users should see "try again in a moment", not an authentication trace.

Rate Limiting

Limit per account rather than per IP, because offices share addresses. For internal tools, a simple in-memory counter is enough; for public apps use a shared store so limits hold across instances.

Frequently Asked Questions

Do I need a database? Only for persistent usage tracking; logs can go to files at first.

How do I stream through Express? Pipe the provider stream to the response and disable buffering.

What about CORS? Restrict origins to your own domain.

How do I handle retries? Retry idempotent calls with backoff; never retry blindly on 4xx.

Should I cache responses? Yes, for repeated questions, with a short expiry.

Conclusion

A proxy is a few dozen lines that prevent the most common and most expensive mistakes. Build it before you need it.

Structuring the Code

Put model calls in one module and keep route handlers thin. The module owns configuration, retries, and logging, so changing provider or model is a single edit. Route handlers validate input and shape the response, which makes them easy to test without hitting the network.

Export a small function such as ask(messages, options) rather than exposing the SDK everywhere. When you later add caching or routing, the change lives in one place instead of dozens.

Handling Streaming

Streaming improves perceived speed but complicates error handling, because the status code is sent before the model finishes. Decide what to do when a stream fails midway: append a visible notice, or mark the message as incomplete so the client can retry.

Deployment Checklist

Set a body size limit, enable compression, configure timeouts, and run behind a process manager that restarts on crash. Verify that logs never contain full payloads with personal data, and rotate keys on a schedule.

Final Note

Read the logs once a week for the first month. You will find payloads larger than expected, retries nobody knew about, and features whose cost does not match their value. Fixing those early keeps the service healthy as traffic grows.

Keep the proxy boring: small, tested, and with one clear responsibility.

An example server route: A POST /chat endpoint receives a message, reads the key from process.env, calls the gateway, then returns the result. The key never reaches the client.

Frequently Asked Questions

Can beginners follow this tutorial?

Yes. The steps are explained in order with code examples, suitable for beginners and experienced developers alike.

Is BankAI’s endpoint compatible with the library I already use?

Yes, BankAI is 100% OpenAI-compatible, so libraries such as the OpenAI SDK and LangChain work without code changes beyond the Base URL and API key.

Where do I get an API key to follow this tutorial?

You get the API key (and Secret Token) after registering and completing payment in the BankAI dashboard.

Sources & References

About the author

Darus S Alfatah — Founder & Writer

Darus S Alfatah is the founder of BankAI. He builds and operates the LLM API gateway that serves Indonesian developers, and writes about LLM integration, gateway architecture, and using AI practically and cost-effectively. Every guide is tested in a real environment — including code, configuration, and token calculations — before it is published, and updated when provider documentation or pricing changes.

Editorial policy

Related articles

🚀 Ready to Use the All-in-One API Key?

Sign up, top up with QRIS, and start building with ChatGPT, Claude, DeepSeek and every other model through one key.

🔑 SIGN UP NOW · Pricing