Application Programming Interfaces (APIs)

An application programming interface (API) is a contract that defines how software components communicate with each other.

  • Consuming an API means requesting or using the data/functions provided by the API.
  • Exposing an API means building an API so others can use it.

An API answers these key questions:

  • What endpoints (URLs) are available?
  • What inputs (parameters or data) do they accept?
  • What outputs (responses) do they return?
  • What errors or exceptions might occur?

REST APIs

POST /login
POST /orders
GET /users/{id}

Representational State Transfer (REST) is a software architectural style for providing standards between computer systems on the web. REST requests are stateless, meaning that each request sent by the client to the server must contain all the information needed for processing, and will not save any client data between requests.

A REST API is an API that adheres to REST architectural constraints. Clients use HTTP requests to interact with resources on a REST API and provide a Uniform Resource Identifier (URI).

GraphQL APIs

GraphQL is an API query language and runtime that enables clients to request only the data they need. Unlike REST, GraphQL allows clients to specify their data requirements. It operates over a single end point and uses a type system to define the capabilities of the API.

Types

type Query {
  # A simple query for a user
  me: User

  # Another example of a query
  getMostPopularCourse(input: GetMostPopularCourseInput): Course
}

# A query will follow the pattern:

type User {
  id: String!
  name: String!
}

type Course {
  id: String!
  name: String!
  majorId: String!
  creditHours: Int!
  tuitionCost: Float!
  deviationFromMeanGPA: Float
  rawDifferenceFromMeanGPA: Float
}

Inputs

input StudentInput {
  id: String!
}

input GetMostPopularCourseInput {
  startDate: String!
  endDate: String!
}

WebSocket APIs

WebSockets are a communication protocol that provides full-duplex (two-way) communication over a single, persistent TCP connection, enabling real-time, low-latency data exchange between client (browser or application) and server, unlike HTTP's request-response cycle. They are crucial for dynamic applications like live chat, gaming, and collaborative editing, allowing servers to push data to clients instantly without constant polling, using an initial HTTP handshake for upgrade. This creates an open channel for continuous data streams, significantly reducing overhead compared to repeated HTTP requests.