Week 9 - Networking and APIs

Introduction to Networking

Client Server Model

TCP/IP Protocol

HTTP Protocol

URL Format

Web APIs

API Testing Tools

Using APIs

AI Responsible Use

Practice

Assignment

Back to core program

What are Web APIs?

An API stands for Application Programming Interface. A Web API is implemented by a web application server through which it makes it services available to client applications.

Think of an API as a menu in a restaurant, and the web server is the kitchen.

A Web API (or Web Service API) is simply an API that is accessible over the HTTP protocol, the same protocol used for viewing websites. It allows different software applications to communicate with each other over the internet.

Public vs Private APIs

There are 2 different types of APIs: public and private APIs.

An API is public when software companies publish parts of their software to be freely used by developers from the outside world. If you were to integrate the Facebook API as a login system in your application, you would be using their API as a public API.

Conversely, there are also private APIs: software companies that grant access to parts of their backend applications to internal developers only, in order to develop new services to be used either internally or for the outside world.

In reality, there are way more private than public APIs. This is because it's usually in the company's best interest to keep their code base hidden from the public eye: it would be like giving your secret recipe away for free.

Keep this in mind: in the real world programming is only a means to serving a business end. In this course you're learning how to program, to make nice-looking, well-functioning applications. However, this is always done within a business context. This is to say: does this software lead to making more money/gaining more popularity/or the achievement of any other business goal?

Important API Concepts

The Core Purpose: Communication

The primary goal of a Web API is to allow a client (like your JavaScript application) to access data or trigger actions on a remote server.

Standardized Exchange: Request and Response

Web API communication is always a two-part standardized cycle:

Step Component Description
1. The Request Client sends data to Server Your application constructs a request, which contains:

The Standard Data Format: JSON

Nowadays, the vast majority of modern Web APIs use JSON (JavaScript Object Notation) as the format for exchanging data in the request and response bodies. (Before JSON took over, another format called XML was commonly used on the web.)

JSON is a human-readable, lightweight format that is native to JavaScript, making it the perfect choice for API development and consumption using tools like fetch.

Example of a JSON Response Body:

{
  "user_id": 101,
  "first_name": "Alex",
  "is_active": true,
  "orders": [
    { "id": 5001, "item": "Widget" },
    { "id": 5002, "item": "Gadget" }
  ]
}

REST (Representational State Transfer)

When you hear the term web API, it is almost always referring to a RESTful API.

Specific URLs at which an API responds to HTTP requests are called endpoints. The table below gives an example of the endpoints of a RESTful API.

Action HTTP Method Endpoint Parameter (id)
Get a list of books GET /books
Get a single book GET /books/{id} 42
Create a new book POST /books
Update book 42 PUT or PATCH /books/{id} 42
Delete book 42 DELETE /books/{id} 42

Basic Security

When working with Web APIs, you will very often be dealing with data that should not be publicly available: user profiles, payment details, private messages, internal business data, and more. To protect this data, APIs use a few common security mechanisms.

Authentication vs Authorization

When working with Web APIs, it is important to distinguish authentication from authorization:

In practice, the server first authenticates the client (for example, via a token), and then authorizes or denies each specific request based on that client’s permissions.

Authentication tokens

Most modern APIs require the client to prove who is making the request.

Instead of sending a username and password with every request, the client sends an authentication token.

GET /profile HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

<aside> ❗

You should never hard‑code real tokens into your frontend code or commit them to Git.

</aside>

API keys

An API key is another kind of secret that identifies which application is calling the API.

GET /weather?city=Amsterdam&apikey=YOUR_API_KEY

or

GET /weather?city=Amsterdam HTTP/1.1
x-api-key: YOUR_API_KEY

<aside> ❗

</aside>

Rate limits

To prevent abuse and to protect their servers, many APIs apply rate limiting.

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 1703858400

When you build applications that call APIs, you should:

Additional Resources

Videos: