JSON & REST APIs
REST APIs
Most web apps talk to a back end through a REST API: URLs that represent resources, with HTTP methods as actions.
| Action | Method + URL |
|---|---|
| List posts | GET /posts |
| Read one | GET /posts/42 |
| Create | POST /posts (JSON body) |
| Update | PUT or PATCH /posts/42 |
| Delete | DELETE /posts/42 |
Filters and pages go in the query string: GET /posts?author=7&page=2&limit=20. |
JSON
The format almost every API uses:
{ "id": 42, "title": "Hello", "tags": ["web", "js"], "published": true, "author": null }
Rules: keys in double quotes, no trailing commas, no comments, no functions.
Reading API docs
Look for the base URL, authentication (API key or Bearer token), each endpoint's parameters, example responses, rate limits and error codes.
CORS
Browsers block requests to other domains unless the server allows it with Access-Control-Allow-Origin headers. A "CORS error" must be fixed on the server, not in your fetch code.
Keep secrets out of the front end
Anything in browser JavaScript is public. Call APIs that need secret keys from your own back end (or a serverless function), never directly from the browser.
Example
// Create, then read, with a free practice API
async function demo() {
const created = await fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title: "My post", body: "Hello API", userId: 1 }),
}).then((r) => r.json());
console.log("Created:", created); // { id: 101, title: "My post", ... }
const res = await fetch("https://jsonplaceholder.typicode.com/posts?userId=1&_limit=3");
const posts = await res.json();
console.log(posts.map((p) => p.id)); // [1, 2, 3]
}
demo().catch(console.error);