📝 Tutorials

What is an API? A Simple Explanation With Examples


An API (Application Programming Interface) is a way for two programs to talk to each other. When you check the weather on your phone, the app sends a request to a weather API, which sends back the data. Your app doesn’t have weather stations — it asks someone who does.

That’s it. An API is a messenger between software.

A real example

You want to show a user’s GitHub profile on your website. You don’t have access to GitHub’s database. But GitHub has an API:

curl https://api.github.com/users/octocat

GitHub responds with:

{
  "login": "octocat",
  "name": "The Octocat",
  "public_repos": 8,
  "followers": 9847
}

Your website reads that JSON and displays it. You never touched GitHub’s database — you just asked their API nicely.

How APIs work (HTTP)

Most APIs use HTTP — the same protocol your browser uses. You send a request, you get a response.

The request has:

  • A URL (where to send it): https://api.example.com/users
  • A method (what to do): GET, POST, PUT, DELETE
  • Headers (metadata): Content-Type: application/json
  • Body (data, for POST/PUT): {"name": "Alice"}

The response has:

  • A status code: 200 OK, 404 Not Found, 500 Server Error
  • Headers: Content-Type: application/json
  • Body (the data): {"id": 1, "name": "Alice"}

REST — the most common API style

REST (Representational State Transfer) is a set of conventions for designing APIs. Most APIs you’ll use are REST APIs.

The pattern: HTTP method + URL = action

MethodURLWhat it does
GET/usersList all users
GET/users/1Get user with ID 1
POST/usersCreate a new user
PUT/users/1Update user 1 (full replace)
PATCH/users/1Update user 1 (partial)
DELETE/users/1Delete user 1

Using an API with JavaScript

// GET — fetch data
const response = await fetch('https://api.example.com/users');
const users = await response.json();
console.log(users);

// POST — send data
const response = await fetch('https://api.example.com/users', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'Alice', email: 'alice@example.com' }),
});
const newUser = await response.json();

Using an API with Python

import requests

# GET
response = requests.get('https://api.example.com/users')
users = response.json()

# POST
response = requests.post('https://api.example.com/users', json={
    'name': 'Alice',
    'email': 'alice@example.com',
})
new_user = response.json()

Using an API with cURL

# GET
curl https://api.example.com/users

# POST
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice"}'

For more cURL patterns, see the cURL cheat sheet.

HTTP status codes

The response includes a number that tells you what happened:

CodeMeaning
200OK — request succeeded
201Created — new resource created
400Bad Request — you sent invalid data
401Unauthorized — you need to log in
403Forbidden — you’re logged in but not allowed
404Not Found — that resource doesn’t exist
429Too Many Requests — you’re being rate limited
500Internal Server Error — the server broke

Full list: HTTP Status Codes cheat sheet

Authentication

Most APIs require authentication so they know who’s making requests.

API Key — a secret string you include in the request:

curl -H "X-API-Key: your-key-here" https://api.example.com/data

Bearer Token (OAuth/JWT) — a token you get after logging in:

curl -H "Authorization: Bearer eyJhbGci..." https://api.example.com/me

REST vs. GraphQL

RESTGraphQL
EndpointsOne per resource (/users, /posts)One endpoint (/graphql)
DataServer decides what to returnClient specifies exactly what it wants
Over-fetchingCommon (get 20 fields when you need 2)No (request only what you need)
Learning curveLowerHigher

Most APIs are REST. GraphQL is popular for complex frontends (like Facebook, which created it).

Public APIs to practice with

Next steps

  1. Open your terminal and run curl https://api.github.com/users/octocat
  2. Try it in JavaScript with fetch()
  3. Build something small — a weather widget, a GitHub profile card
  4. Learn about authentication when you’re ready for private APIs