API Contracts and Product Behavior
Lesson 5Beginner1h 10mAssessment-backed

API Contracts and Product Behavior

Learn how APIs express product behavior, protect service boundaries, and become reliable contracts between clients and systems.

What you will be able to do

Explain an API as a product contract, not just a URL.
Design request and response shapes around user behavior.
Use status codes, validation, pagination, idempotency, and versioning deliberately.
Understand how API contracts map to AWS, GCP, and Azure gateway patterns.
Avoid API designs that leak database internals or create fragile clients.

An API is the contract between a client and a system. Good APIs make product behavior clear, failure behavior predictable, and service ownership enforceable.

An API Is a Contract

A weak API exposes whatever the database currently looks like. A strong API exposes what the product allows a client to do, what the client must provide, what the system promises in response, and what can go wrong.

API contract between client product behavior and service internals
The API boundary should protect clients from internal service and database changes.
Contract partQuestionExample
EndpointWhat action is allowed?POST /v1/notifications
Request bodyWhat must the client provide?recipientId, channel, templateId, variables
Response bodyWhat does the system promise?notificationId, status, acceptedAt
Status codesHow does failure show up?400 invalid input, 401 unauthenticated, 429 rate limited
IdempotencyWhat if the client retries?Same idempotency key should not create duplicate notifications.

Scenario: Checkout API

Imagine an ecommerce checkout flow. The client wants to place an order. The system must validate cart items, address, payment intent, inventory, discounts, and shipping method. A single vague endpoint can hide too much complexity.

Bad contractProblemBetter contract thinking
POST /checkout with arbitrary cartClient can send invalid product stateServer validates product IDs, quantities, price version, and address.
Returns only success trueClient cannot guide the user after failureReturn explicit error codes like OUT_OF_STOCK or PAYMENT_ACTION_REQUIRED.
Creates order on every retryDuplicate orders during network failuresRequire idempotency key for order creation.
Exposes internal table IDs everywhereLocks client to storage internalsExpose stable public IDs and product concepts.

Request and Response Design

Request fields should match what the client can truthfully know. Response fields should match what the client needs to render the next product state. Do not force clients to understand internal workflows.

Design choiceGood practiceWhy
NamesUse product languageClients understand orderId better than database row key.
ValidationReject impossible states earlyPrevents bad data from entering deeper services.
ErrorsUse stable machine-readable error codesClients can show correct UI and retry behavior.
PaginationUse cursor pagination for changing listsAvoids duplicates and missing items during updates.
VersioningPreserve old contract until clients migrateMobile and external clients cannot update instantly.

Design principle

Design APIs from user flows and client behavior first. Storage tables, queues, and provider SDKs are implementation details behind the contract.

API Standards That Matter

Standard areaWhat to decideProduction reason
AuthenticationWho is calling?Protects tenant and user data.
AuthorizationWhat can they do?Prevents privilege escalation.
ValidationWhat input is accepted?Stops invalid state at the boundary.
Rate limitingHow much can one client call?Protects shared infrastructure.
IdempotencyWhat happens on retry?Prevents duplicate writes.
ObservabilityWhat request metadata is tracked?Enables debugging and SLO tracking.

Cloud API Entry Points

After the contract is clear, cloud services can implement authentication, routing, throttling, logging, and deployment. The contract should remain understandable without knowing the provider.

NeedAWSGCPAzure
API gatewayAPI Gateway or Application Load BalancerAPI Gateway or Cloud Load BalancingAPI Management or Application Gateway
Serverless handlerLambdaCloud Functions or Cloud RunAzure Functions or Container Apps
Container serviceECS/EKSCloud Run/GKEContainer Apps/AKS
Auth integrationCognito/IAM/JWT authorizersIdentity Platform/IAM/IAPMicrosoft Entra ID/API Management policies
ObservabilityCloudWatch/X-RayCloud Monitoring/TraceAzure Monitor/Application Insights

Beginner Mistakes

  • Designing endpoints around database tables instead of user actions.
  • Returning vague errors that clients cannot act on.
  • Forgetting idempotency for write APIs that clients may retry.
  • Ignoring pagination until lists become large.
  • Changing API response shapes without thinking about old clients.

Guided Practice

Practice task

Design the API contract for creating a support ticket. Define endpoint, required fields, response fields, three error codes, and whether idempotency is needed.

Sample Answer

PartExample
EndpointPOST /v1/support-tickets
Required fieldssubject, description, category, requesterId
ResponseticketId, status, createdAt, nextExpectedResponseAt
ErrorsINVALID_CATEGORY, REQUESTER_NOT_FOUND, RATE_LIMITED
IdempotencyUse an Idempotency-Key header so a retry does not create duplicate tickets.

Before You Continue

  • You should understand an API as a product contract.
  • You should be able to define request, response, errors, and retry behavior.
  • You should know why APIs should not leak database internals.
  • You should be ready to follow what happens after a request enters the system.