Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Decoding API Contracts

Recommended resources

Hello! Welcome to your third lesson.

In our previous session, we demystified the REST API request-response cycle. You learned about the fundamental mechanics of how an application (the client) asks for information and how a server responds. We covered the key components: endpoints, methods, headers, status codes, and the JSON body.

Today, we'll build directly on that knowledge. Now that you understand how the conversation happens, we're going to learn how to read the "rulebook" that governs it: the API contract. For you as a designer and future architect, this is like being handed the blueprint of a building. It reveals the structure, the available materials, and the constraints you need to work within to design a functional and resilient system.

This brings us to our learning outcome for this lesson: Interpret a simplified API contract to identify key data models, endpoints, and error types.

1. What is an API Contract?

An API contract is the official documentation that describes how an API behaves. Think of it as a technical user manual that provides a definitive, unambiguous description of the service. It tells developers, designers, and architects everything they need to know to interact with the API correctly.

A contract formally specifies:

  • What functions are available.
  • How to call them.
  • What data you need to provide.
  • What data you will get back.

The industry standard for creating these contracts for REST APIs is the OpenAPI Specification (formerly known as Swagger). While these contracts are written in machine-readable formats like JSON or YAML, they are structured to be understandable by humans.

To see how OpenAPI fits into this picture, watch this short video.

REST API and OpenAPI: It’s Not an Either/Or Question

This video from IBM Technology introduces the concept of an OpenAPI definition as a file that describes what a REST API can do, making it understandable for both humans and machines.

Watch from 01:10 to 04:37. Focus on how an OpenAPI definition acts as a standardized format for describing an API's resources, endpoints, and operations.

Now, for a more formal definition, let's turn to a short reading.

API Contracts - an Extended Introduction

This article, 'API Contracts - an Extended Introduction,' explains what API contracts are and the role they play. It sets the stage for why they are so crucial in system design.

Read the section titled 'What Are API Contracts?'. Don't worry about the different specification names (like gRPC or GraphQL for now); just focus on understanding the purpose and general contents of a contract.


2. Deconstructing an API Contract

An API contract can seem dense at first, but it can be broken down into a few key areas of interest. For your role, the three most important things to identify are the endpoints, the data models, and the error types. Let's use an excellent article to guide us through these components.

How to Read API Documentation

The article 'How to Read API Documentation' from Department of Product provides a clear, practical breakdown of the essential parts of an API contract. We will use it as our primary reference for this section.

Keep this article open. We'll be referring to its specific sections as we explore each component of an API contract.

a. Identifying Endpoints (The "Where" and "What")

As we covered in the last lesson, an endpoint is the URL where your request is sent, and the method (GET, POST, etc.) defines the action. The API contract serves as a directory of all available endpoints and the methods permitted for each one.

API Endpoints for Banking Services
This image illustrates how a single API service can expose multiple endpoints, each corresponding to a distinct function like 'Fraud Detection' or 'Loan Default Prediction'. The contract would detail each of these.

Read the section titled "API endpoints" in the "How to Read API Documentation" article. It provides real-world examples from Mailchimp and Spotify, showing how endpoints are structured to give you access to different functionalities.

b. Identifying Data Models (The "Shape" of the Data)

This is arguably the most critical part for a UX designer. Data models define the structure and format of the data you work with. They are the blueprint for the JSON objects in the request and response bodies.

A data model tells you:

  • What information you can get from the API (e.g., a user object includes a name, email, and creation_date).
  • What information you must provide to the API (e.g., to create a new product, you must send a name and price).
  • The data type for each piece of information (e.g., name is a string, price is a number, is_active is a boolean).

In API documentation, these models are often described under the "Resources" you can access or as part of the "Request format" and "Response format".

Now, please read the following sections in the "How to Read API Documentation" article:

  1. "Resources": This section explains how APIs group data into logical "bundles" like Addresses, Products, or Users. This is a high-level way of thinking about data models.
  2. "Request format": This shows how the contract specifies the data model required for sending information to the API.
  3. "Response format": This shows how the contract specifies the data model you'll get back from the API.

c. Identifying Error Types (Handling Failures Gracefully)

A robust system isn't just about what happens when things go right; it's about how it behaves when things go wrong. A good API contract explicitly defines the possible failure scenarios for each endpoint.

In the last lesson, you learned about the different classes of status codes (2xx, 4xx, 5xx). An API contract gets more specific. For a given endpoint, it will list the exact status codes you can expect and what they mean in that context.

For example, when trying to create a new user with POST /users:

  • 400 Bad Request: Could mean the email format is invalid.
  • 409 Conflict: Could mean a user with that email already exists.

As a designer, knowing these specific error types allows you to create precise, helpful error messages and UI states, guiding the user to a resolution.

Read the section "Response codes" in the "How to Read API Documentation" article to review the meaning of the main status code categories.


3. A Practical Example

Let's put this all together by interpreting a simple, human-readable API contract for a hypothetical weather service. This example comes from the "API Contracts - an Extended Introduction" article you looked at earlier.

API Contract Excerpt:

GET /weather

Input parameters:
- location (string): The location for which to retrieve weather information (e.g. "New York, NY")
- version (string): The version of the API to use (e.g. "v1", "v2")

Output:
- temperature (float): The current temperature in degrees Fahrenheit
- condition (string): A description of the current weather conditions (e.g. "sunny", "cloudy", "rainy")

Example usage:
GET /weather?location=New%20York,%20NY&version=v2

Output:
{ 
  "temperature": 72.5, 
  "condition": "sunny" 
}

Excerpt from "API Contracts: Best Practices" on Bump.sh

Let's analyze this using our framework:

  1. Endpoint:

    • The endpoint is clearly defined as /weather.
    • The HTTP method to be used is GET.
  2. Data Models:

    • Input Data Model: The contract specifies two required input parameters: location (a string) and version (a string). This is the data the client sends.
    • Output Data Model: The contract specifies the structure of the data the client receives. It's a JSON object containing temperature (a float) and condition (a string). This tells a designer exactly what information they have available to display on a weather screen.
  3. Error Types:

    • This simplified example doesn't explicitly list error codes. A more complete contract would include a "Responses" section detailing what happens if the location isn't found (404 Not Found) or if a parameter is missing (400 Bad Request).

By spending just a minute reading this contract, you've learned exactly how to get weather data, what you need to provide, and what you'll get back. This is the power of being able to interpret an API contract.


Conclusion

You've now learned how to read the "blueprint" of an API. This skill is a crucial bridge between user experience design and system architecture. It empowers you to understand the technical capabilities and constraints of a system, enabling you to design interfaces that are not only user-friendly but also technically feasible and robust.

Key Takeaways:

  • An API contract is the formal documentation describing how an API works. OpenAPI is the standard for creating these contracts.
  • You can interpret a contract by looking for three key elements:
    1. Endpoints: The available URLs and their corresponding HTTP methods (GET, POST, etc.).
    2. Data Models: The structure of the data for both requests (inputs) and responses (outputs), often defined as "Resources".
    3. Error Types: The specific status codes (like 404 or 409) an endpoint can return and what they mean.
  • Understanding these components allows you to have informed discussions with engineers and to design UIs that are perfectly aligned with the backend data and logic.

In our next lesson, we will shift gears from technical architecture to economic modeling. We'll start with the fundamentals by learning how to create a simple spreadsheet to model the costs and benefits of building a new feature, another critical skill for a systems architect.