Using AI through APIs
How apps and scripts send requests to AI models in code instead of a chat window.
What is it?
An API (application programming interface) lets your own software talk to an AI model. Your code sends a request with instructions and input, and the provider's server sends back the model's answer. This is how many apps add AI features without training their own models.
APIs usually need an account and a secret key, and they are typically billed by usage, often by the number of tokens processed. Developers can set options such as which model to use, how long the answer may be and what format it should have.
Which models use it?
- OpenAI offers an API for its GPT and other models.
- Anthropic offers an API for its Claude models.
- Google offers Gemini models through its developer API and through Google Cloud.
- Cloud platforms such as Amazon Bedrock, Google Cloud and Microsoft Azure provide access to several model families.
- Many providers publish official software libraries for languages such as Python and JavaScript.
How to use it
- Create an account with a provider and read its getting-started guide.
- Create an API key and keep it secret: store it on a server or in a secure setting, never in public code or inside a mobile app.
- Send a simple test request using the provider's official library.
- Add your system prompt, the user's input and any settings such as maximum length.
- Handle errors, timeouts and rate limits, and consider a fallback model.
- Set spending limits and monitor usage and cost.
Benefits
- Add AI features to your own app, website or workflow.
- Choose and switch between models without retraining anything.
- Pay for what you use instead of buying hardware.
- Automate repetitive text tasks at scale.
Limits and risks
- Costs can grow quickly with heavy use if limits are not set.
- Leaked API keys can be abused and charged to you.
- Providers may change models, prices or terms.
- User data sent to an API is processed by the provider, so you must follow privacy rules and tell users.
Prompt template
Copy it, replace the parts in [brackets] and paste it into any AI chat.
I am building [type of app] in [programming language]. I want to call an AI model API to [feature]. Explain the request and response flow, show a minimal example using the provider's official library with the API key read from an environment variable, and list the error cases I must handle (timeouts, rate limits, invalid output). Do not invent library functions; tell me which parts I should check in the official documentation.
More in Building with AI
- AI agents and tool useAI systems that can take actions, such as searching, running code or using apps, to finish a task.
- MCP (Model Context Protocol) and connectorsAn open standard for connecting AI assistants to tools and data sources in a consistent way.
- RAG (retrieval-augmented generation)Letting an AI answer from your own documents by finding relevant passages first.
- Embeddings and semantic searchTurning text into numbers that capture meaning, so you can search by idea rather than exact words.
- Fine-tuning vs promptingWhen to customize a model with extra training, and when better prompts are enough.
- Evaluating AI outputPractical ways to check whether AI answers are accurate, useful and safe before you rely on them.