Safeguard
Developer Tools•

How do you write an OpenAPI spec that helps security testing?

Declare security schemes, constrain inputs, list every operation and define errors. A better OpenAPI file gives scanners and reviewers more to work with.

Safeguard Security Team
3 min read

Write the spec so a tool can learn your rules: declare how each operation is authenticated, constrain every input with types and limits, include admin and error responses, and name your environments. A precise OpenAPI file lets scanners and reviewers test your intent, not guess at it.

Why the spec is a security artifact

The OpenAPI Specification is a standard interface description for HTTP APIs, designed so people and computers can discover a service's capabilities without reading source code. Scanners, gateways, test generators and code generators all read it. Whatever it leaves vague, they leave vague.

Checklist

Declare security explicitly

Define security schemes once, then apply requirements to operations. Know the spec's rules: only one of the listed security requirement objects needs to be satisfied, an empty requirement object makes security optional, and an empty top level array removes a global requirement. Review any operation that uses either form.

Constrain inputs

FieldWhat to set
StringsMaximum length, a pattern or format where one applies
NumbersMinimum and maximum
ArraysMaximum item count
EnumerationsAllowed values only
PaginationMaximum page size
Request bodiesForbid unknown properties on write operations

These limits support OWASP API4, which lists records per page and maximum input sizes among the limits an API should enforce.

List every operation

Include admin and support routes, not just public ones. Omitting them keeps scanners away from the most sensitive functions, and OWASP warns that administrative endpoints often sit beside regular ones.

Define errors and responses

Describe error responses and their schema. OWASP API8 advises defining and enforcing response schemas, including errors, so stack traces and sensitive details never reach clients. Document 401, 403, 413, 415 and 429 where they apply.

Describe servers and environments

List the real server URLs per environment so tests target staging by design. Record the version in the path or server entry.

Give realistic examples

Provide example values for IDs and enumerations. Scanners that use defaults for parameters will perform better with sensible examples.

Keep it honest

Generate or verify the file in CI. A tidy spec that drifts from the code is worse than none, because people trust it. See OpenAPI spec drift.

Try it yourself

Click through it at your own pace, or open it full screen. See what inputs a scan needs about each endpoint.

Quick check

Should the public spec include admin endpoints?

Keep a complete internal spec for testing. For anything published to outside consumers, OWASP advises restricting documentation to authorized users.

Which OpenAPI version should I use?

Use one your tools support. The spec site listed 3.2.1 as latest on 19 October 2026, but check what your scanner and generators read.

Safeguard's free scan at gold.safeguard.sh/scan is a quick way to check the dependencies behind the API.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.