Your draft script now calls ask(), and the module does the rest. It picks the provider, looks up the model, and sends your key in the Authorization header of every request. Back when you set up the project, you put that key in .env, kept the file out of Git, and stopped logging settings. Those habits stop the most common leak. They don’t tell you what a stranger could do with the key, where else it can slip out, or what to do on the day it does.
In this lesson, you’ll see what the provider checks when a request arrives, why a key never goes into a web page’s code, and how to respond when a key leaks. You’ll also see how one key per project keeps a leak small. Start with how the provider tells who’s calling.
See What a Key Lets Anyone Do
The provider can’t see your laptop, your name, or your script. All it gets is an HTTP request, and the only part of it that says who you are is the key after the word Bearer. The word fits because the key works for whoever bears it. A secret that grants access to anyone who presents it, with no password or second check, is called a bearer credential.
You can watch the provider turn away a key it doesn’t recognize. In the lesson Load Settings From the Environment, you saw that a variable set in the shell wins over the one in .env. Use that rule to send one request with a made-up key, without touching your real one. Open the version for your provider:
Set OPENAI_API_KEY to a made-up value for this one run:
The request reaches OpenAI, and the adapter’s error check stops the script with the provider’s message:
file:///.../model/openai.js:24
...
Error: 401: Incorrect API key provided: sk-proj-***************1234. You
can find your API key at https://platform.openai.com/account/api-keys.
...
Set ANTHROPIC_API_KEY to a made-up value for this one run:
The request reaches Anthropic, and the adapter’s error check stops the script with the provider’s message:
file:///.../model/anthropic.js:25
...
Error: 401: invalid x-api-key
...
The message names x-api-key, the header older code used, but it’s about the key your adapter sent in Authorization: Anthropic rejects a bad key the same way in either header.
The 401 status means authentication failed. It says nothing about the rest of the request, which might still fail with a valid key. The same check works against you. A key like yours, with no extra restrictions set on it, works as well from a stranger’s computer: their request drafts a reply and adds the cost to your bill. Your spending limit roughly caps how much a stranger can spend, but it can’t tell their requests apart from yours.