SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

System design
API ContractsRESTGraphQLRPCChoosing an API contract
API design

API Contracts

Describe an API contract and compare resource, field, and operation-based interfaces.

Lesson goal

Specify the inputs, successful result, and error behavior a client needs, then recognize three ways to express that contract.

Before you begin — HTTP.

A weather app needs to display the temperature for a city. The team building the screen should be able to tell what to send, what units it will receive, and what to show for an unknown city. Those decisions form part of an application programming interface (API), the contract one software component exposes to another.

Follow a request against a contract

Suppose the weather service accepts a city identifier and returns a temperature in degrees Celsius. It also defines a response for an unknown city. The app can format a successful reading or display the documented error. The service can change how it stores observations as long as it keeps those promises.

A weather app sends a request through the API contract and receives a response from the weather service.

Think through the contract in order. Input tells the caller how to identify the city. Output specifies the field, its type, and its units. Error behavior tells the caller how to distinguish an unknown city from a successful reading of zero degrees. A numeric field alone would leave the caller guessing.

Identify the boundary

A local function such as Math.max and an HTTP endpoint both have interfaces. A local library call can run inside the calling program. A request to a remote service crosses a network boundary and can time out while the service is still working.

A local API runs on the caller’s machine. A network API sends the request to a remote service.

Knowing the boundary helps a designer choose what failures the contract must describe. A valid city identifier is enough to construct a weather request. Receiving a timely reply also depends on the network and the remote service.

Express the same read three ways

Suppose a client needs user 42’s name. Each of the next lessons expresses that operation with a different contract.

StyleWhat the client specifiesWhere to see it
RESTA resource URI and an HTTP method, such as GET /users/42.REST
GraphQLFields from a typed schema, such as the name belonging to user 42.GraphQL
RPCA named operation and arguments, such as GetUser(42).RPC

A service can also publish events or stream updates when clients need to hear about changes as they happen. The right contract follows from what the caller needs to do.

Check your understanding

The weather service changes its storage engine this week. Next week it keeps the same response field but starts reporting Fahrenheit. Which change affects the app?

Compare your reasoning

The app can keep using the same contract after the storage change. Switching units changes the meaning of a successful response. The app would display the wrong temperature even though it could still parse the number.

Key Takeaways

  • A contract defines the meaning of inputs, results, and errors for callers.
  • The boundary determines which failures can occur between issuing a call and receiving its result.
  • REST, GraphQL, and RPC organize client interactions around resources, selected fields, and named operations.

How is this lesson?

Previous lesson From a hostname to an HTTP responseNext lessonREST
Edit on GitHubReport on GitHub
On this page
%

On this page

Follow a request against a contractIdentify the boundaryExpress the same read three waysCheck your understandingKey Takeaways