API Layers and Middleware

Write down what a request to your app must look like, then check the order of the steps that run on it.

Content date: October 15, 2026.

Plain English

An API is an agreement between whoever sends a request and your app: what can be asked for, what each input means, and what an answer or error looks like. A club sign-up form sends a name, email, and club to the app's /signups address and gets back "saved" or "here is what's wrong." Middleware is code that runs on a request before your main code, like a front desk: it reads the data, confirms who is calling, and limits size. Steps run in the order they are registered, so a check placed after the action it guards protects nothing.

Use this when

Use this when a browser, another service, or a scheduled job calls your app's code. First get clear on what runs in the browser and what runs on the server ("Frontend vs Backend").

If you are just starting

Write down one endpoint: its method (GET reads, POST creates, PATCH changes, DELETE removes), accepted fields, success answer, and each failure. Accept only the fields you named. Check types and lengths, and return every error in one shape, such as {"error": "Email looks invalid"}. Use the framework you already have.

When your project grows

Before changing a response, decide what existing callers, like an older mobile app, can count on. Set a maximum request size, decide who must be logged in, and check permission per record. Decide how a retried request, like a double-pressed Sign up, is handled. Keep stack traces out of public errors, but log a request ID.

Worked example

The sign-up endpoint accepts name, email, and clubId. A test request also sends isApproved=true, which the form never sets. The contract rejects unexpected fields with a 400 error (ignoring them quietly is fine if documented). Either way, the endpoint picks the three fields by name instead of copying the whole body, so no caller can approve themselves.

Practice

Build an endpoint contract table

  1. Pick one endpoint in a local project and note who calls it and what it does.
  2. Write example requests and responses for one success and three failures.
  3. List the middleware and route in the order the code runs them.
  4. Send each example to the running local app and fix the code or the table wherever the result differs.

Deliverable: A request and response table, the middleware order, and the actual result for each example.

Common mistakes to watch for

  • The form checks that grade is a number, but the saving code reads the raw request, so grade "abc" gets saved.
  • /signups requires login, but a new /signups/export route skips that step and returns every student's email.
  • A request adds role=admin, the app copies the whole body into the user record, and a student becomes admin.
  • A failed save returns status 200, so the form says "You're signed up!" and the student has no spot.

Prompting

A weak prompt

Add an endpoint that accepts whatever the frontend sends.

A better prompt

Define and build one endpoint in this project's framework, [for example, POST /signups]. State the method, allowed fields, who must be logged in, record permissions, and the success, validation-error, denied, and server-failure responses. Show the middleware order. Test a valid, malformed, unauthorized, and database-failure request with made-up data. Keep existing callers working, or say what would break.

A written contract turns "it works when I click" into testable cases, including failures a happy-path click hides.

Check before you move on

  • Every input has a type and a size limit.
  • Unexpected fields have a stated rule.
  • Login and permission checks run before data changes.
  • Errors don't expose internals.
  • You listed who already calls this endpoint.

You are done when

  • Each case in your table gives the status and response you wrote down.
  • A reviewer can see where each check runs before data changes.

Questions to think about

Your login-check middleware is correct, yet a visitor reaches a members-only page without logging in. What do you check?

Its order and coverage. It may be registered after the route, or a second route may skip it.

A shortcut copies the whole request body into the new sign-up record. Why is that risky?

The sender controls the body, so extra fields like role can change things they shouldn't. Pick the allowed fields and validate each.

A save fails, but the server replies 200 with a message in the body. What goes wrong?

The form treats the reply as success, and the lost sign-up is never recovered. Send a clear error status the form can react to.

Official references