API design process and best practices

Factor
Top-Down (Design-First)
Bottom-Up (Code-First)
Focus
User/Consumer Needs
Existing System/Code
Starting Point
API Specification/Contract
Business Logic/Data Models
Strength
Excellent user experience, stability, parallel development.
Speed of implementation, simplicity for internal projects.
Weakness
Slower initial start-up time.
Risk of creating a messy or unintuitive API.
Best For
Public-facing APIs, complex systems, multi-team projects.
Internal APIs, rapid prototypes, simple data exposure.
image
image
image

The API design process, as explained in the video, involves several key stages (1:30:6):

  1. Understanding Requirements: This is the first step, where you identify the core use cases and user stories for your API. You also define the scope and boundaries of what the API will do, determine performance needs (like identifying bottlenecks), and consider security constraints.
  2. Design Approaches: You can choose different ways to approach the design:
    • Top-down: Start with high-level requirements and define endpoints and operations from there.
    • Bottom-up: Design based on existing data models and capabilities if they are already in place.
    • Contract-first: Define the API contract (what requests and responses look like) before starting implementation.
  3. Life Cycle Management: APIs aren't just coded and forgotten. The process includes:
    • Design Phase: Discussing requirements and expected outcomes.
    • Development & Testing: Building and locally testing the API.
    • Deployment & Monitoring: Deploying the API to staging or production environments and monitoring its performance and usage.
    • Maintenance: Ensuring the API remains functional and easy to update, which is why simplicity in design is crucial.
    • Deprecation & Retirement: Eventually, older versions of APIs may be phased out as new ones are introduced.

Both top-down and bottom-up are valid strategies for API design. The choice between them depends on the project's priorities, the needs of the API's consumers, and the existing system architecture.

Here’s a breakdown of each approach:

Top-Down Approach (Design-First)

In a top-down approach, the design of the API comes first, before any code is written.1 The process starts by defining the needs of the client or end-user.

  • Process:
    1. Define the Contract: You begin by creating a detailed specification or "contract" for the API.2 This is often done using a specification language like OpenAPI (formerly Swagger) or API Blueprint.3 This contract describes every endpoint, the expected requests, the potential responses, data models, and error codes.4
    2. Gather Feedback: This design document is shared with all stakeholders (front-end developers, mobile developers, partners, etc.) for feedback.5 This ensures the API will meet everyone's needs before development resources are invested.
    3. Build to Spec: Once the contract is finalized, the front-end and back-end teams can work in parallel.6 The back-end team implements the logic to fulfill the contract, while the front-end team can build against a mock server that simulates the API's behavior based on the contract.
  • When to Use It:
    • When the user experience is the top priority.
    • When multiple teams or external partners will be consuming the API.
    • For large, complex, or long-term projects where stability and consistency are critical.
    • When creating a new product or system from scratch.
  • Analogy: This is like an architect creating a detailed blueprint for a building before any construction begins. The needs of the occupants (the users) dictate the design.

Bottom-Up Approach (Code-First)

Conclusion