API Design

What is API Design?

API design is the process of making intentional decisions about how an API will expose data and functionality to its consumers. A successful API design describes the API's endpoints, methods, and resources in a standardized specification format.

The API design process benefits both consumers and producers by ensuring that APIs support business objectives while remaining easy to use, adaptable, testable, and well-documented. API design should occur early in the API lifecycle in order to achieve alignment among key stakeholders and to help teams identify issues before they become ingrained. API design is also an important part of an effective API governance strategy, as it helps teams standardize API patterns that can be reused across their organization.


API Lifecycle

This lifecycle definition isn't meant to match the needs of every enterprise organization but is intended to provide you with an example to help you think about what your own API lifecycle looks like.

API Lifecycle

8 stages of the API lifecycle

  • Define
    • Ensure API operations are well-defined, with clear responsibility.
    • Dedicated team workspaces and public GitHub repositories.
  • Design
    • Create consistent API designs using OpenAPI and mock servers.
    • Use collaborative comments to guide development.
  • Develop and Document
    • Provide complete, accurate documentation with examples.
    • Ease API onboarding and usage.
  • Test
    • Implement manual and automated testing.
    • Ensure APIs function as expected with contract and performance tests.
    • Contract tests can be derived from API artifacts like OpenAPI, JSON Schema, and GraphQL, and used to ensure there are no changes to what has been promised when an API was designed or developed.
  • Secure
    • Apply consistent authentication and security testing.
    • Protect against vulnerabilities.
  • Deploy
    • Use CI/CD pipelines for consistent deployment.
    • Leverage API gateways for deployment across environments.
  • Observe
    • Monitor APIs with contract, performance, and security tests.
    • Track changes and performance over time.
  • Distribute for Discovery
    • Enable search and discovery of APIs.
    • Improve access for both producers and consumers within private or public networks.

API-first development model

API-first is a development model in which applications are conceptualized and built with services that are delivered through APIs. Whereas companies that take a code-first approach might view APIs as afterthoughts, API-first companies design their APIs before they develop their applications. This strategy enables consumers and producers to collaborate on API definitions before the implementations are built out, which improves both the quality and usability of APIs.


What are the key stages of API design?

The key stages of API design are:

  • Step 1: Determine what the API is intended to do
    • The use case may also have implications on the type of architecture you choose. For instance, a gRPC-based architecture might make the most sense for an API that connects internal microservices, while a GraphQL API would be well-suited for a service that relies on disparate data sources
  • Step 2: Define the API contract with a specification
    • Which resources are required, how their data should be formatted and structured, how they should relate to one another, and which methods should be available on their associated endpoints.
  • Step 3: Validate your assumptions with mocks and tests
    • Mocks can also be used alongside API tests, which can be run manually, on a schedule, or automatically within CI/CD pipelines
  • Step 4: Document the API
    • This step, which involves defining key details about every resource, method, parameter, and path, helps validate the design and ensure that consumers are able to start using your API as quickly as possible

What are some API design best practices?

  • Prioritize consistency
    • It's crucial for leaders to establish an API governance strategy that promotes organization-wide standards
    • For instance, every API in an organization's portfolio should use consistent naming conventions for every method, endpoint, and resource
  • Gather input from every stakeholder
    • API design issues often stem from poor communication. Each stakeholder is likely to have domain-specific knowledge that may impact the API's design and implementation, and they should be included in every conversation
  • Understand the API's context and constraints
    • Teams must have a clear understanding of an API's context and constraints before they make any design decisions

What is RESTful API design?

RESTful API design is the process of designing an API that follows the principles of Representational State Transfer (REST), which is the most popular API architecture today. In a RESTful architecture, resources are identified by URIs (Uniform Resource Identifiers), and the client interacts with those resources with standard HTTP methods such as GET, POST, PUT, and DELETE.

  • POST
    • Pros:
      • Hides parameters in the request body, reducing the chance of sensitive data being exposed in URLs.
      • Can handle larger payloads since it’s not limited by URL length restrictions.
      • Not cached by default, minimizing the risk of sensitive data being stored unintentionally.
    • Cons:
      • Not idempotent, meaning multiple identical POST requests can create duplicate resources or perform unintended actions.
      • Can be less predictable than PUT for resource creation or updates.
  • GET (Read)
    • Pros:
      • Simple and efficient for retrieving data from the server.
      • Cached by browsers and proxies, improving performance for repeated requests.
      • Data is visible in the URL, making it easy to share or bookmark requests.
    • Cons:
      • Exposes query parameters in the URL, which can lead to security issues if sensitive data is included.
      • Limited in the amount of data it can send due to URL length restrictions.
      • Often cached, which can expose outdated or sensitive data unintentionally.
  • PUT (Update)
    • Pros:
      • Idempotent, meaning that multiple identical requests result in the same effect, reducing the risk of unintended changes.
      • Explicitly updates or replaces a resource at a known location, offering greater control.
      • More predictable for updating existing resources.
    • Cons:
      • Requires the client to send the full representation of the resource, which may be less efficient than PATCH for partial updates.
      • Overwriting a resource may lead to data loss if not carefully handled.
  • DELETE
    • Pros:
      • Idempotent, meaning repeated DELETE requests do not have additional side effects.
      • Simple to use for removing resources from the server.
    • Cons:
      • Destructive by nature, so proper authentication and authorization are critical to avoid accidental or unauthorized deletions.
      • No easy undo or rollback mechanism if a resource is deleted unintentionally.
  • PATCH (Partial Update)
    • Pros:
      • Allows partial updates to a resource, making it more efficient than PUT when only a small part of the resource needs to change.
      • Ideal for making minor updates without sending the entire resource.
    • Cons:
      • Less predictable than PUT since only part of the resource is updated, increasing the risk of inconsistent data states if validation is poor.
      • Not idempotent, so repeated PATCH requests can have unintended effects if not managed carefully.

API technologies

  • REST
    • Pros: Stateless: Each request from a client to a server must contain all the information needed to understand and complete the request. Uses standard HTTP methods (GET, POST, PUT, DELETE). Easily consumable by different clients (browsers, mobile apps).
    • Cons: This can lead to over-fetching or under-fetching of data- because More endpoints may be required to access specific data.
  • GraphQL
    • Pros: Allows clients to request exactly what they need, avoiding over-fetching and under-fetching. Strongly typed schema-based queries.
    • Cons: Complex queries can impact server performance. All requests are sent as POST requests.
  • gRPC
    • Pros: Built on HTTP/2, it provides advanced features like multiplexing and server push. Uses Protocol Buffers, a language-neutral, platform-neutral, extensible way of serializing structured data. Efficient in terms of bandwidth and resources, especially suitable for microservices.
    • Cons: Less human-readable compared to JSON. Requires HTTP/2 support.
  • SOAP
    • Pros: Highly extensible with support for security, transactions, and reliability. Widely accepted standard for communication in distributed systems, used in many legacy and enterprise systems.
    • Cons: Verbose due to XML usage, which can increase network traffic. More complex compared to other standards.
  • WebSockets
    • Pros: Enables full-duplex communication between client and server over a single, long-lived connection. Ideal for real-time applications like chat, gaming, and live updates.
    • Cons: Requires maintaining open connections, which can be resource-intensive. Not suitable for short, simple request-response interactions.
  • Server-Sent Events (SSE)
    • Pros: Allows a server to push updates to the client via a single, long-lived HTTP connection. Simpler to implement than WebSockets for server-to-client updates.
    • Cons: One-way communication from server to client only. Limited to HTTP/1.1 and may not perform as well under high concurrency compared to WebSockets.
  • Webhooks
    • Pros: Allows one service to notify another service in real-time when an event occurs. Simple and efficient for event-driven architectures, commonly used in integration scenarios.
    • Cons: Relies on the availability of the receiving service, with no built-in retry mechanism. Requires manual setup of endpoints to receive notifications.
  • AMQP (Advanced Message Queuing Protocol)
    • Pros: Provides reliable, scalable messaging across distributed systems. Supports message queuing, routing, and persistence. Commonly used in enterprise systems and microservices.
    • Cons: More complex to configure and manage than simpler protocols like HTTP. Requires message brokers like RabbitMQ.
  • EDI (Electronic Data Interchange)
    • Pros: Standardized format for exchanging business documents (e.g., invoices, purchase orders) between organizations. Reduces manual processing and errors in business transactions.
    • Cons: Can be costly to implement and maintain. Requires expertise to set up and integrate with existing systems.
  • EDA (Event-Driven Architecture)
    • Pros: Promotes loose coupling between services by allowing them to react to events asynchronously. Highly scalable and resilient, suitable for distributed, microservice-based systems.
    • Cons: Can introduce complexity in managing event flows and debugging. Requires robust monitoring and error-handling mechanisms.

API Governance Strategy

API governance is the application of rules to promote a consistent set of API behaviors across the company’s API landscape. Traditionally, these are performed by a manual review process, thus making them error-prone and inconsistent. In addition, errors are identified late in the API development lifecycle impacting the productivity of development teams and causing production incidents.


API Versioning

API versioning is the process of managing and tracking changes to an API. It also involves communicating those changes to the API's consumers.

Effective API versioning strategy not only enables API producers to iterate in a way that minimizes the consumer-facing impact of breaking changes, but also provides a framework for effectively communicating these changes to consumers. This transparency builds trust and—in the case of public APIs—strengthens the organization's reputation, which can boost the API's adoption and retention rates.

When to version an API

You should version your API whenever you make a change that will require consumers to modify their codebase in order to continue using the API.

Some common examples of breaking changes include:

  • Renaming a property or endpoint
  • Turning an optional parameter into a required parameter
  • Modifying a data format or type
  • Modifying a property's characteristics

Types of API Versioning

There are several approaches to API versioning, including:

  • URL versioning
    • For instance, consumers who are interested in viewing all of the products in a database would send a request to the https://example-api.com/v1/products endpoint. This is the most popular type of API versioning.
  • Query parameter versioning
    • This strategy requires users to include the version number as a query parameter in the API request. For instance, they might send a request to https://example-api.com/products?version=v1.
  • Header versioning
    • This approach allows consumers to pass the version number as a header in the API request, which decouples the API version from the URL structure.
  • Consumer-based versioning
    • This versioning strategy allows consumers to choose the appropriate version based on their needs. With this approach, the version that exists at the time of the consumer's first call is stored with the consumer's information. Every future call is then executed against this same version—unless the consumer explicitly modifies their configuration.

Backward Compatibility and Versioning: When modifying endpoints, it's important to maintain backward compatibility. This means ensuring that changes don't break existing clients.

Backward Compatibility

Rate Limits and CORS

Another best practice is to set rate limitations. This is used to control the number of requests a user can make in a certain timeframe. This is crucial for maintaining the reliability and availability of your API. It also prevents the API from DDoS attacks.

Common practice is to also set CORS settings Cross-Origin Resource Sharing (CORS) settings are important for web security. They control which domains can access your API, preventing unwanted cross-site interactions.


Resource Links Used


Postman - API Design
Postman - API Lifecycle Blueprint
Postman - API Governance
Postman - API-First Guide
Postman - What Is a REST API?
Medium - API Design 101: From Basics to Best Practices
Mocks Server
Postman - API versioning