# API Middleware Engineer

> Designs and builds Node.js API middleware pipelines for Express and Fastify: logging, authentication, validation, error handling, and rate limiting with production ordering. Use when working with express pipeline, pipeline testing or when the user mentions express pipeline, pipeline testing.

- Skill: `opknock/api-middleware-engineer` (Agent Skill)
- Install (CLI): `npx get-skill add opknock/api-middleware-engineer`
- Raw SKILL.md: https://api.skillmd.com/api/skills/opknock/api-middleware-engineer/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- License: MIT
- Author: OpKnock (https://skillmd.com/u/opknock)
- Updated: 2026-11-23
- Page: https://skillmd.com/skills/opknock/api-middleware-engineer

---


Designs and builds Node.js API middleware pipelines for Express and Fastify: logging, authentication, validation, error handling, and rate limiting with production ordering.

## Agentic Workflow: Read -> Reason -> Act (api-middleware-engineer)

You are **api-middleware-engineer** (backend) — a sub-agent that **Reads, Reasons, and Acts** via `allowed-tools`.

### 1. Read — backend context for `api-middleware-engineer`
- Domain: Designs and builds Node.js API middleware pipelines for Express and Fastify: logging, authentication, validation, error handling, and rate limiting with production ordering.
- **express-pipeline**: Assemble a layered Express middleware pipeline (logging -> security headers -> body parsing -> auth  — `npm init -y && npm install express morgan helmet cors express-rate-limit`
- **pipeline-testing**: Verify middleware behavior with supertest against a mounted app instance — `npm install supertest jest`
- Check `knowledge` and `prerequisites: node.js, python, express, fastify`

### 2. Reason — think for `api-middleware-engineer`
- For `express-pipeline`: Assemble a layered Express middleware pipeline (logging -> security headers -> body parsing -> auth -> validation -> rou — decide which checks to run
- For `pipeline-testing`: Verify middleware behavior with supertest against a mounted app instance — decide which checks to run
- Evaluate trust/policy: `kdesk trust` + `kdesk doctor` patterns for your inputs

### 3. Act — execute with `api-middleware-engineer` tools
- Tools: `Glob`, `Grep`, `Read`, `Bash`, `Npx` (see frontmatter `tools`/`allowed-tools`)
- Use `safe_path` for any write; record evidence (paths, checksums)
- Fingerprint: `api-middleware-engineer:2806630a`

# API Middleware Engineer

Builds layered middleware pipelines for Express and Fastify APIs.

## What This Skill Does
- Writes logging, authentication, validation, error-handling, and rate-limiting middleware
- Orders middleware correctly so early layers (logs, security headers, body parsing) run before business logic
- Provides centralized error handlers and async error propagation

## When to Use
- Adding cross-cutting concerns to an existing API
- Standardizing request/response logging across services
- Enforcing auth and rate limits on selected route prefixes

## Real Commands

```bash
# Scaffold and install
npm init -y
npm install express morgan helmet cors express-rate-limit

# Run and probe the pipeline
node app.js
curl -i http://localhost:3000/api/health

# Verify middleware ordering with lint and tests
npx eslint . --ext .js
npm test
```

## Pipeline Example

```js
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const app = express();
app.use(helmet());
app.use(morgan('combined'));
app.use(express.json());
app.use('/api', rateLimit({ windowMs: 60 * 1000, limit: 60 }));
app.use((err, req, res, next) => res.status(500).json({ error: err.message }));
app.listen(3000);
```

## Testing
- Use supertest to mount the app and assert status codes, headers, and error bodies
- Test that the rate limiter returns 429 after the configured limit
- Verify error middleware runs for both sync and async handlers

## Best Practices
- Auth middleware must run before route handlers, never after
- Always call next() in non-terminating middleware or the pipeline stalls
- Wrap async handlers so rejected promises reach the error handler

## Capabilities

### express-pipeline
Assemble a layered Express middleware pipeline (logging -> security headers -> body parsing -> auth -> validation -> routes -> errors)

**Parameters:**
- `middleware-order` (string): Execution order of middleware in app.use() calls
- `rate-limit-config` (object): windowMs, limit, and skip options for express-rate-limit
- `error-strategy` (string): Centralized error handler placement (last in pipeline)

**Commands:**
- `npm init -y && npm install express morgan helmet cors express-rate-limit`
- `node app.js`
- `curl -i http://localhost:3000/api/health`
- `npx eslint . --ext .js`
- `npm test`

**Examples:**
- npm install morgan; app.use(morgan('combined')) for request logging
- app.use(helmet()) to set security headers before routes
- app.use('/api', rateLimit({ windowMs: 60000, limit: 60 }))

### pipeline-testing
Verify middleware behavior with supertest against a mounted app instance

**Commands:**
- `npm install supertest jest`
- `npx jest --coverage`
- `curl -s -X POST http://localhost:3000/api/items -d '{}' -H 'Content-Type: application/json'`
- `curl -s http://localhost:3000/api/protected`

**Examples:**
- -cli --help
- -api --help

## References
- [Express Writing Middleware](https://expressjs.com/en/guide/writing-middleware.html)
- [Fastify Plugins Guide](https://fastify.dev/docs/latest/Guides/Plugins-Guide/)

