What Is an API and How to Use One in Your First Website
Understand what an API is and how to call one from your first website with fetch, handle JSON responses, manage errors, and keep your secret API keys safe.

The first time a beginner pulls live weather, a random quote, or a list of products into their own page, web development suddenly clicks: your site is no longer a static brochure but a window onto other systems. An API, or application programming interface, is a defined set of rules that lets one piece of software request data or actions from another, usually by sending an HTTP request to a URL and receiving structured data, most often JSON, in return. This guide explains how APIs work and walks you through using one safely in your first website.
Quick Answer: An API is a contract that lets your website ask another service for data over HTTP. To use one, read its documentation, find the endpoint URL, call it with JavaScript's fetch function, convert the response with response.json(), and display the result on the page. Keep secret API keys on a server, never in front-end code.
How an API Request Actually Works
An API request works like ordering from a menu: you choose an item the menu offers, send a request in the expected format, and receive a predictable response. The documentation is the menu, and anything not listed simply is not available.
Most web APIs follow the REST style, where data is organized into resources addressed by URLs. An endpoint is a specific URL for a resource, such as https://api.example.com/products/42. The method describes what you want: GET reads data, POST creates it, PUT or PATCH updates it, and DELETE removes it. Once these ideas feel natural, the best way to deepen them is to build your own Express API and see the same request from the server's side.
Every request can carry headers, which are metadata such as authentication tokens or the content type, and some carry a , which holds data you are sending. The server answers with a and usually a JSON body. JSON, or JavaScript Object Notation, is a text format of keys and values that maps neatly onto JavaScript objects, which is why it dominates web APIs.



