Analyst Bootcamp Join on TelegramJoin
Junior+ · Systems Analysis

From a user story to an OpenAPI contract

🕥 10 min read🧭 SA📅 updated 2026-10-15Читать на русском

The system analyst's most common deliverable: turn a story and its acceptance criteria into an API contract that the frontend, the backend and the tester can build against before any code exists. Resources, verbs, fields, errors, and the five questions that decide the shape.

The story and the contract

As a customer, I want to save a delivery address so that I do not retype it next time.

AC: up to 5 addresses; postcode validated for the shipping country; one address can be default; deleting the default makes the most recent one default.

The frontend developer needs to know the endpoint, the fields and the errors to build the screen. The backend developer needs the same to build the handler. The tester needs it to write cases. If the contract is written first, all three work in parallel; if it is not, they meet in a pull request two weeks later and argue.

That contract is an OpenAPI document: a YAML file that describes paths, operations, request and response schemas, and error responses. You do not need to write the whole file by hand (developers and tools fill in details), but you need to write enough that the shape is decided.

Step 1: find the resources

Nouns in the story become resources; resources get paths. Here: address belongs to a customer, so the natural path is a collection under the customer:

GET    /customers/{customerId}/addresses          list
POST   /customers/{customerId}/addresses          create
GET    /customers/{customerId}/addresses/{id}     read one
PATCH  /customers/{customerId}/addresses/{id}     update (partial)
DELETE /customers/{customerId}/addresses/{id}     delete

Two decisions were just made, and both are worth a sentence in the ticket:

"Make default" could have been its own action (POST /addresses/{id}/default). Keeping it as a field on PATCH is simpler, and the acceptance criterion about re-assigning the default on delete becomes server behaviour, not a client responsibility.

Step 2: the schema, field by field

Every field gets a type, whether it is required, and its constraints. The constraints are the acceptance criteria in machine-readable form:

components:
  schemas:
    AddressInput:
      type: object
      required: [line1, city, postalCode, country]
      properties:
        label:      { type: string, maxLength: 40, example: "Home" }
        line1:      { type: string, maxLength: 120 }
        line2:      { type: string, maxLength: 120 }
        city:       { type: string, maxLength: 80 }
        postalCode: { type: string, maxLength: 16 }
        country:    { type: string, pattern: "^[A-Z]{2}$", description: ISO 3166-1 alpha-2 }
        isDefault:  { type: boolean, default: false }
    Address:
      allOf:
        - $ref: "#/components/schemas/AddressInput"
        - type: object
          required: [id, createdAt]
          properties:
            id:        { type: string, format: uuid, readOnly: true }
            createdAt: { type: string, format: date-time, readOnly: true }

Separating AddressInput (what the client sends) from Address (what the server returns) is the single habit that prevents the most arguments: the client never sends an id, and the server always returns one.

Step 3: the operation, with every answer it can give

An operation is not defined by its happy path. List each response the client must handle:

paths:
  /customers/{customerId}/addresses:
    post:
      summary: Save a delivery address
      operationId: createAddress
      parameters:
        - { name: customerId, in: path, required: true, schema: { type: string, format: uuid } }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/AddressInput" }
      responses:
        "201":
          description: Address saved
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Address" }
        "400":
          description: Validation failed (invalid postcode for country, field too long)
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Problem" }
        "403": { description: Not the customer's own account }
        "409":
          description: Address limit reached (max 5)
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Problem" }

Reading the responses back against the acceptance criteria: "up to 5" became 409; "postcode validated" became 400 with a reason; "own addresses only" became 403. Every criterion now has an HTTP answer, which is what the tester will check.

The story's nouns become paths, its rules become constraints and error responses, and the contract goes to three people at once.
The story's nouns become paths, its rules become constraints and error responses, and the contract goes to three people at once.

Step 4: the error body

One error shape for the whole API, decided once. RFC 9457 ("Problem Details") is the standard, and it is small:

{
  "type": "https://api.example.com/problems/validation",
  "title": "Validation failed",
  "status": 400,
  "detail": "postalCode is not valid for country DE",
  "errors": [{ "field": "postalCode", "code": "invalid_for_country" }]
}

The code per field is what the frontend uses to show the right message in the right language; the human text in detail is for logs and developers. Write the list of codes this operation can return into the ticket; it is short and it is the part the frontend asks for first.

Step 5: the five questions that decide the shape

Before you call the contract done, answer these in the ticket. Each one changes the YAML.

  1. Who may call it, on whose data? Path parameter and 403 rule. Here: the authenticated customer, own id only; support staff with a role, any id.
  2. What is the limit, and what happens at it? 409 with a code, or silently replace the oldest? The acceptance criteria say limit; the ticket must say which.
  3. Is it idempotent? POST that creates on every call is dangerous on a flaky mobile network. Either accept an Idempotency-Key header or make the client use PUT with a client-generated id. Say which (and see the next topics).
  4. What does the list return, and in what order? Pagination is unnecessary for five items, but the order (default first, then most recent) is a rule and belongs in the description.
  5. What changes elsewhere? Deleting the default re-assigns it; the order checkout reads the default. Those are two lines in the ticket and one line in the DELETE description.

What you hand over

With AI

Models produce syntactically valid OpenAPI from a story in seconds, and it is a good first draft for the boring parts (schemas, examples, parameter blocks). They default to generic choices: PUT where PATCH is right, no 409, no error codes, no idempotency. Answer the five questions yourself, then let the model expand the YAML, then review every response code against the acceptance criteria.

Try it (5–10 minutes)

Story: "As a customer, I want to add items to a wish list so that I can buy them later." AC: max 50 items; an item can be in the list once; adding an out-of-stock item is allowed but flagged; the list is private.

  1. Write the paths and verbs. Decide: is "add item" a POST to a collection or a PUT with the product id? Say why.
  2. Write the input and output schemas with constraints, and the responses for the add operation, including 409 for duplicates and the limit.
  3. Answer the five questions in five lines. Which one changed your YAML?

Where this shows up in real work

  • System analyst and API-focused BA job posts ask for "designing REST APIs" and "writing OpenAPI/Swagger specifications"; the take-home is usually a story to turn into endpoints.
  • In real teams the contract-first ticket is what lets frontend and backend start the same day; teams without it lose a week per feature in integration.
  • Freelance integration briefs ("connect our shop to X") are won by the reply that comes with a draft contract instead of questions.

Sources worth reading

Practice it on real drills

REST API design for a featureReview a draft API contractEnd-to-end feature spec (business + system)

Finished reading and did the exercise?