Skip to content

Where should I put an API key in a Next.js or browser application?

Keep the AISwapLab key on your server, outside browser bundles and public environment variables. Let your signed-in user call your own backend, which checks ownership and spending approval before calling AISwapLab. A hidden form field, minified JavaScript or a mobile app binary does not make an embedded credential private.

PROTECT KEYS AND ACCOUNT ACCESSAISwapLab team · Editorial methodPublished 2 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: Separate public and private settings; Authorize each task; Protect result delivery.
Workflow diagram: Separate public and private settings → Authorize each task → Protect result delivery. Read the article for evidence and limits.

Your backend must enforce the user boundary

The key belongs to your AISwapLab account and can spend its shared balance within the allowed scope. If every visitor can trigger unrestricted requests through your proxy, moving the key off the page alone is insufficient. Your application still needs authentication, authorization, rate controls and a durable user-to-job association.

What to do next

  1. 01

    Separate public and private settings

    Expose only non-secret display configuration to the frontend. Store the key in the server's secret environment and verify its presence without logging its value.

  2. 02

    Authorize each task

    Check the current application user, approved model and credit ceiling before submitting. Do not accept an arbitrary account owner or unrestricted endpoint from the browser.

  3. 03

    Protect result delivery

    Map the returned job to the requesting user in your database. Check that mapping before your backend downloads or streams private content to a client.

A concrete example

Illustrative architecture: the browser sends an approved internal task ID to your backend. The backend loads that user's saved payload and uses its private key. The browser never receives the bearer secret or unrestricted upstream access.

Before you call it ready

  • No key reaches a public bundle.
  • The proxy checks authenticated user ownership.
  • Generation approval cannot be bypassed by editing browser fields.

When to stop or choose another method

If the key has already shipped in a public build, revoke it and deploy a replacement kept server-side. Removing the visible source string alone does not recover secrecy.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation