Environment Setup


Hard-coding API keys directly in source code is a security risk. The recommended approach is to store your key in an environment variable and load it at runtime.


Creating a .env file

Create a .env file in the root of your project:

SOLROUTER_API_KEY=sr_YOUR_API_KEY

Always add .env to your .gitignore so it is never committed to version control:

echo ".env" >> .gitignore

Loading the key in Node.js

Node.js 20.6+ can load .env files natively with the --env-file flag:

node --env-file=.env index.js

For older Node.js versions, install the dotenv package:

npm install dotenv

Then load it at the top of your entry file before any other imports:

import "dotenv/config";

const apiKey = process.env.SOLROUTER_API_KEY;

Loading the key in Python

Install python-dotenv:

pip install python-dotenv

Then load the .env file at the start of your script:

from dotenv import load_dotenv
import os

load_dotenv()

api_key = os.environ["SOLROUTER_API_KEY"]

Loading the key in Next.js

Next.js has built-in .env support — no extra packages needed. Create a .env.local file (which is already git-ignored by the Next.js default .gitignore):

SOLROUTER_API_KEY=sr_YOUR_API_KEY

Access it in server-side code (API routes, Server Actions, Route Handlers):

const apiKey = process.env.SOLROUTER_API_KEY;

Important: Never expose your key to the browser. In Next.js, only variables prefixed with NEXT_PUBLIC_ are bundled into the client. Your SOLROUTER_API_KEY must not have that prefix.


Loading the key in Deno

Deno has built-in support for .env via std/dotenv:

import { load } from "https://deno.land/std@0.224.0/dotenv/mod.ts";

const env = await load();
const apiKey = env["SOLROUTER_API_KEY"];

Or pass it directly as an environment variable when running:

SOLROUTER_API_KEY=sr_... deno run --allow-env --allow-net index.ts

Using a secrets manager in production

For production workloads, prefer a dedicated secrets manager over .env files:

PlatformServiceHow to inject
AWSSecrets Manager / Parameter StoreVia Lambda env vars or ECS task definitions
GCPSecret ManagerMounted as env vars in Cloud Run
AzureKey VaultReferenced in App Service configuration
VercelEnvironment VariablesSet in Project → Settings → Environment Variables
RailwayVariablesSet in Service → Variables panel
Fly.ioSecretsfly secrets set SOLROUTER_API_KEY=sr_...

Next steps

  • First Request — make your first API call
  • API Keys — key management, rotation, and security practices