In this lesson
Putting AI inside the app is easy. Doing it without leaking the key that pays the bill is what matters.
So far you have used AI to build. Now the app will use AI on the inside: a button that summarizes an email, classifies a message or suggests a reply. For that it needs an API key.
- Know what an API key is and how to limit spending
- Understand why the key never goes in the browser code
- Get a "summarize" button working with the key kept outside the files
Chat and API: two ways to pay
In the chat, you pay a subscription and you talk. With the API, it is your program that talks to the AI, and you pay for what you use, measured in tokens (pieces of text). The app sends the request together with a key, the password to your account. They are separate accounts.
- Monthly limit. Set the cap on the account before you create the key.
- A small model for a small task. Summarizing an email does not need the most expensive model.
- Count the calls. A button clicked a hundred times a day is a hundred calls.
Check the current prices on the provider's official page.
Real example
An accounting firm gets dozens of long emails a day. A "summarize and list what the client is asking for" button saves minutes on each one. With a small model and a monthly limit, the cost stayed low and predictable.
The rule with no exceptions
Everything in a site's HTML, CSS or JavaScript is downloaded by every visitor. Anyone can open the source code and read it. If the key is in there, anyone can copy it and spend on your behalf. That is why the key lives on the side the visitor does not see.
| Setup | Where the key lives |
|---|---|
| Local use (this lesson) | An environment variable on your computer, outside the folder |
| Published | In a server function at the hosting service |
| Local model (Ollama) | There is no key and no cost per use |
Careful
If the AI suggests a .env file in the project folder, accept it only if the .gitignore already excludes it (lesson 9). When in doubt, use the system environment variable.
The request
You ask for two things: the page with the button and a small server that holds the key. The request tells the AI to read the key from the environment variable and ends with an audit: the AI confirms that the key is not in any file. Then do your own: open the files and search for a piece of the key with Ctrl + F.
The text the app sends leaves your computer and goes to the provider's servers. Send the minimum, replace CPF numbers (Brazil's taxpayer ID) and account numbers with placeholders, and read the provider's data policy.
What the pros know
Not every task needs an answer right away. Summarizing a thousand emails from the week, for example, can wait. Anthropic's official documentation describes a batch API (Message Batches) that cuts the cost by 50%, and says that most batches finish in less than an hour. The trade is simple: you give up the immediate answer. Before you pay a lot for a button that gets clicked all day long, ask: does this need to happen now?
Practice · 10 min
- On the provider's site, set the monthly limit and create the key. Keep it in your password manager.
- Open the tool in a new folder and paste the request below. Follow the instructions to set the environment variable and start the server.
- Test with three made-up texts and then audit the files, looking for the key.
Summary
- Chat is a subscription. The API is paid per use, with a key.
- Set the monthly limit before you create the key.
- The key never goes in a file the browser downloads.
- The app sends the minimum, because the text leaves your computer.
Sources
Read on October 30, 2026.
- Anthropic. Batch processing: 50% lower cost; most batches finish in less than 1 hour.
Lesson quiz
Three situations to apply what you learned.
Isaque Victor free courses