Avancé20 min de lecture

API Documentation & Testing

Master API documentation with OpenAPI/Swagger, learn testing strategies, and discover tools that make APIs easy to understand and use.

Why Documentation Matters

An API without documentation is like a library without a catalog. Developers cannot use what they cannot understand. Even the most elegant, well-designed API is worthless if nobody knows how to call it.

Good API documentation answers these questions instantly:

  • What endpoints exist? (GET /users, POST /orders, etc.)
  • What parameters do they accept? (query params, path params, request body)
  • What does the response look like? (structure, data types, example values)
  • How do I authenticate? (API key, OAuth, JWT)
  • What errors might I get? (400, 401, 404, 500 — and what they mean)
  • Are there rate limits? (100 requests per minute, etc.)

Bad documentation is worse than no documentation. If your docs are outdated, incomplete, or confusing, developers will waste hours debugging, then give up and use a competitor's API instead.

Real-world example: Stripe is famous for exceptional API documentation. Every endpoint has clear descriptions, request/response examples in multiple languages (curl, Python, Ruby, Node.js), inline code snippets you can copy-paste, and interactive API explorers. This is a major reason Stripe dominates payment processing — developers love using their API.

The gold standard for API documentation today is OpenAPI (formerly Swagger). It lets you describe your entire API in a structured format, then generate beautiful interactive documentation, client libraries, and even mock servers automatically.

Investing in documentation is investing in developer experience (DX). A well-documented API gets adopted faster, generates fewer support tickets, and builds trust with developers. Documentation is not an afterthought — it is part of the product.

OpenAPI Specification

OpenAPI is a standard format for describing RESTful APIs. You write a YAML or JSON file that describes every endpoint, parameter, request body, response, and error. Tools can then read this file and generate documentation, SDKs, tests, and mock servers.

Here is a simple OpenAPI spec for a user API:

yaml
openapi: 3.0.0
info:
  title: User API
  version: 1.0.0
  description: API for managing users

paths:
  /users:
    get:
      summary: List all users
      parameters:
        - name: limit
          in: query
          schema:
            type: integer
            default: 10
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
    post:
      summary: Create a new user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/NewUser'
      responses:
        '201':
          description: User created

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        email:
          type: string
    NewUser:
      type: object
      required:
        - name
        - email
      properties:
        name:
          type: string
        email:
          type: string

This single file describes:

  • Two endpoints: GET /users and POST /users
  • A query parameter (limit) with a default value
  • Request and response schemas
  • HTTP status codes
  • Reusable schemas (User, NewUser)

Key sections of an OpenAPI spec:

  • info — Metadata about the API (title, version, description)
  • servers — Base URLs for the API (https://api.example.com)
  • paths — Every endpoint, organized by path
  • components/schemas — Reusable data models (User, Post, Error, etc.)
  • components/securitySchemes — Authentication methods (API key, OAuth, JWT)

Why use OpenAPI?

  • Single source of truth — One file describes the entire API
  • Automated docs — Tools like Swagger UI and Redoc turn the spec into beautiful interactive docs
  • Client generation — Generate SDKs for JavaScript, Python, Java, Go automatically
  • Validation — Validate requests/responses against the spec to catch bugs
  • Mock servers — Generate a mock API for frontend development before the backend is ready

Many frameworks can generate OpenAPI specs automatically from your code (FastAPI for Python, NestJS for Node.js, Springdoc for Java). You write the code, the framework generates the spec, and you get documentation for free.

Documentation Tools

Once you have an OpenAPI spec, you need to turn it into user-friendly documentation. Here are the most popular tools:

1. Swagger UI

The classic OpenAPI documentation tool. Displays your API as an interactive website where developers can read docs and try out API calls directly in the browser. You can send real requests, see real responses, and experiment with parameters.

Pros:

  • Interactive — test endpoints without leaving the docs
  • Widely recognized (developers know how to use it)
  • Free and open source

Cons:

  • Looks dated compared to modern tools
  • Not as customizable

2. Redoc

A beautiful, modern alternative to Swagger UI. Generates clean, responsive documentation from OpenAPI specs. Great for public-facing API docs.

Pros:

  • Gorgeous design (looks professional)
  • Three-panel layout (menu, content, examples)
  • Supports complex schemas well

Cons:

  • Read-only (no interactive testing)
  • Requires more setup than Swagger UI

3. Postman

A full-featured API development tool. Import your OpenAPI spec into Postman and get a complete collection of requests. Save requests, organize them into folders, write tests, and share collections with your team.

Pros:

  • Powerful testing and automation features
  • Team collaboration (shared workspaces)
  • Environment variables (dev, staging, prod)
  • Can generate documentation from collections

Cons:

  • Not automatic (manual request creation unless importing OpenAPI)
  • Heavy tool (overkill for simple APIs)

4. Stoplight Studio

A visual OpenAPI editor and documentation platform. Design your API visually, get instant documentation, and collaborate with your team.

Pros:

  • Visual editor (no need to write YAML by hand)
  • Built-in mock servers
  • Collaboration features

Cons:

  • Paid product (free tier is limited)

Which to use?

  • Swagger UI — Best for internal APIs or MVP documentation
  • Redoc — Best for public-facing APIs where design matters
  • Postman — Best for teams doing active API development and testing
  • Stoplight — Best for larger teams or API-first companies

Many companies use multiple tools: Swagger UI for internal testing, Redoc for public docs, and Postman for QA and automation.

API Testing Strategies

Documentation tells developers how the API should work. Testing ensures it actually works. There are several levels of API testing:

1. Manual Testing (curl / Postman)

The simplest approach: make requests manually and inspect responses.

bash
# Test user creation
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice", "email": "[email protected]"}'

# Test authentication
curl https://api.example.com/users \
  -H "Authorization: Bearer YOUR_TOKEN"

Postman provides a GUI for the same thing: create requests, organize them into collections, save them for reuse.

Pros: Fast, flexible, great for exploration and debugging Cons: Not repeatable, doesn't catch regressions, doesn't scale

2. Automated API Tests

Write code that calls your API and asserts the responses are correct. Run these tests on every deployment.

javascript
// Example with Jest
test('GET /users returns array of users', async () => {
  const response = await fetch('https://api.example.com/users');
  const users = await response.json();
  
  expect(response.status).toBe(200);
  expect(Array.isArray(users)).toBe(true);
  expect(users[0]).toHaveProperty('id');
  expect(users[0]).toHaveProperty('name');
});

test('POST /users creates a new user', async () => {
  const response = await fetch('https://api.example.com/users', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: 'Bob', email: '[email protected]' })
  });
  
  expect(response.status).toBe(201);
  const user = await response.json();
  expect(user.name).toBe('Bob');
});

Pros: Catches regressions, runs on CI/CD, ensures API works as expected Cons: Requires writing and maintaining test code

3. Contract Testing

Ensures your API matches its OpenAPI spec. Tools like Dredd or Schemathesis read your spec and automatically generate tests.

Pros: Docs and API stay in sync (if the spec is wrong, tests fail) Cons: Requires maintaining an OpenAPI spec

4. Load Testing

Tests how your API performs under heavy load. Tools like k6, Locust, or Apache JMeter simulate thousands of concurrent users.

Pros: Finds performance bottlenecks before production Cons: Requires infrastructure to run load tests

Best practice: Use all of these. Manual testing for exploration, automated tests for confidence, contract testing for accuracy, and load testing before launch.

Example: API Testing with fetch

html
<h3 style="font-family:sans-serif;">Testing a Public API</h3>

<button onclick="testAPI()" style="padding:10px 20px; background:#3b82f6; color:white; border:none; border-radius:6px; cursor:pointer; font-size:14px;">
  Test JSONPlaceholder API
</button>

<div id="results" style="margin-top:20px; padding:15px; background:#f1f5f9; border-radius:8px; font-family:monospace; white-space:pre-wrap; font-size:13px;"></div>

<script>
async function testAPI() {
  const output = document.getElementById('results');
  output.textContent = 'Running tests...\n\n';

  try {
    // Test 1: GET request
    output.textContent += '✓ Test 1: GET /users/1\n';
    const userRes = await fetch('https://jsonplaceholder.typicode.com/users/1');
    const user = await userRes.json();
    output.textContent += `  Status: ${userRes.status} (expected 200)\n`;
    output.textContent += `  User name: ${user.name}\n\n`;

    // Test 2: POST request
    output.textContent += '✓ Test 2: POST /posts\n';
    const postRes = await fetch('https://jsonplaceholder.typicode.com/posts', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        title: 'Test Post',
        body: 'This is a test',
        userId: 1
      })
    });
    const post = await postRes.json();
    output.textContent += `  Status: ${postRes.status} (expected 201)\n`;
    output.textContent += `  Created post ID: ${post.id}\n\n`;

    // Test 3: Error handling
    output.textContent += '✓ Test 3: GET /posts/99999 (not found)\n';
    const notFoundRes = await fetch('https://jsonplaceholder.typicode.com/posts/99999');
    output.textContent += `  Status: ${notFoundRes.status} (expected 404)\n\n`;

    output.textContent += '✅ All tests passed!';
  } catch (error) {
    output.textContent += `\n❌ Error: ${error.message}`;
  }
}
</script>

What is the primary purpose of the OpenAPI specification?

Prêt à pratiquer ?

Crée ton compte gratuit pour accéder à l'éditeur de code interactif, lancer les défis et suivre ta progression.