Using OpenRouter with Replit
Replit lets you build, deploy, and host applications directly in the browser. You can use OpenRouter with Replit in two ways:- Replit Agent (BYOK) — Use your own OpenRouter API key when Replit Agent builds AI features for you, instead of the Replit-managed default.
- Replit Apps (project Secrets) — Store your OpenRouter API key as a Secret in any Replit App and call OpenRouter from your code.
Prerequisites
You’ll need an OpenRouter API key. Create one at openrouter.ai/keys and copy the key (it starts withsk-or-v1-).
Using your OpenRouter API key with Replit Agent
By default, when Replit Agent detects that your prompt needs AI functionality, it uses Replit-managed credentials and bills usage to your Replit account. If you’d rather use your own OpenRouter account — for direct billing, a custom rate limit, or access to a wider set of models — you can bring your own API key.For new Replit Apps
When you submit your initial prompt to create a new app, include that you want to use your own OpenRouter API key. For example:Build a chat app that uses my own OpenRouter API key.In the Agent’s response, the provider name will appear without the “(Replit managed)” suffix, which indicates Agent will request your OpenRouter API key. Agent typically builds through the first checkpoint and then prompts you for the key.
For existing Replit Apps
When Agent detects that you want to add AI functionality to an existing app, it shows a confirmation prompt asking to use Replit AI Integrations with Replit-managed credentials. To use OpenRouter directly instead:- Click Dismiss on the integration prompt.
- Let Agent build through the first checkpoint.
- When Agent asks for an API key, paste your OpenRouter key from openrouter.ai/keys.
OPENROUTER_API_KEY) and wire it into the generated code.
Adding your OpenRouter API key as a Replit Secret
If you’re writing code in a Replit App yourself (without going through Agent), add your OpenRouter API key as a project Secret so it’s available as an environment variable at runtime.- Open your Replit App.
- In the left sidebar, open the Secrets tool (it looks like a padlock icon).
- Click New Secret.
- Set the Key to
OPENROUTER_API_KEY. - Set the Value to your OpenRouter API key (for example,
sk-or-v1-...). - Click Add Secret.
process.env.OPENROUTER_API_KEY (Node.js) or os.environ["OPENROUTER_API_KEY"] (Python). Secrets are not exposed in your source files and are scoped per project.
Calling OpenRouter from your code
Once the secret is configured, you can call OpenRouter using any OpenAI-compatible SDK by pointing it athttps://openrouter.ai/api/v1.
Replit Deployments and Teams
When you publish a Replit App as a Deployment, the Secrets you configure in your Replit App carry over to the deployed app. Update theOPENROUTER_API_KEY secret in the Deployment’s settings if you want to rotate keys without redeploying code.
For Teams and Enterprise organizations on Replit, your organization admin controls whether Replit AI Integrations is enabled. If it’s disabled — or if you prefer not to use Replit-managed credentials — every member can still bring their own OpenRouter API key using the steps above.