What Is an API Schema? Define Required Fields and Types for JSON Requests
An API schema is a machine-readable contract that describes the structure and constraints of data an API accepts or returns. For JSON APIs, JSON Schema can define an object's properties, types, required fields, and value limits; a validator checks whether a JSON instance follows those rules. Schema validation checks data shape. It does not guarantee authentication, business rules, or the server's overall behavior. This article uses JSON Schema Draft 2020-12 to define a product-creation request.
An API schema is a machine-readable contract that describes the structure and constraints of data an API accepts or returns. For JSON APIs, JSON Schema can define an object's properties, types, required fields, and value limits; a validator checks whether a JSON instance follows those rules. Schema validation checks data shape. It does not guarantee authentication, business rules, or the server's overall behavior. This article uses JSON Schema Draft 2020-12 to define a product-creation request.
What problem does an API schema solve?
A schema makes the data contract for a request or response precise. It can say whether name is a string, price is a number greater than zero, tags is a list of strings, and which properties must be present. Schemas can be reused for documentation, input validation, and client-code generation, but check which schema draft and keywords each tool supports. (Getting started with JSON Schema, JSON Schema specification)
How do you write a product-creation input schema?
The example defines the JSON data contract for a product-creation request. The values are illustrative and do not describe a real API or pricing policy.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.invalid/schemas/create-product.json",
"title": "CreateProductRequest",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"price": { "type": "number", "exclusiveMinimum": 0 },
"tags": { "type": "array", "items": { "type": "string" } }
},
"required": ["name", "price"],
"additionalProperties": false
}$schema identifies the JSON Schema dialect used by this document, and $id gives the schema resource a URI identifier. type: object requires the top-level value to be an object. properties defines rules by property name but does not make those properties required. Only name and price appear in required; tags may be omitted. additionalProperties: false rejects unlisted properties, so consider compatibility between clients and servers before choosing that policy. (Objects and required properties)
Here is an example of a valid instance:
{ "name": "Notebook", "price": 12.5, "tags": ["paper", "office"] }This schema rejects a price of zero or less, a missing name, or a non-string value inside tags. Sending null for name is also different from omitting it: null is not a string. To allow null, define an explicit type combination for the draft you use. These checks concern structure and declared constraints; they do not determine whether a product with that name exists or whether the requester is allowed to create it.
How are required, null, and extra properties different?
Decide these three conditions separately. First, required controls whether a property must be present. Second, type constrains the value when the property exists. A required string still cannot contain null, because null is not a string. Third, additionalProperties controls keys not listed in the schema. JSON Schema allows unspecified properties by default, so add a restriction if they must be rejected. (JSON Schema object rules)
Rejecting extra keys can reveal misspelled fields early, but an older client may also reject a new server field. Allowing unspecified keys can make extensions easier, while letting typos pass unnoticed. Choose based on request or response direction, rollout order, and version policy, then check with real consumers and validators.
What can schema validation establish, and what can it not?
A validator takes a schema and a JSON instance and reports whether the instance meets the declared constraints. Different programming languages can validate against the same rules, but verify the selected draft, format-validation settings, and vocabulary support. A keyword such as format may be treated as an annotation or validated with different levels of strictness, depending on the implementation. (How validators work, JSON Schema Validation Draft 2020-12)
A schema alone cannot guarantee permissions, inventory, database state, or business rules that depend on other fields or server state. It can check that price is positive, for example, but server logic must separately determine whether the requester may sell at that price or whether stock remains. A successful validation means the request has an acceptable shape under the contract; it does not guarantee that the server will return success.
How do OpenAPI and JSON Schema relate?
OpenAPI describes HTTP API paths, operations, parameters, responses, and security. Its Schema Object describes input and output data types. The default Schema Object dialect in OAS 3.2.1 is based on JSON Schema Draft 2020-12 and adds OAS vocabulary. Standalone JSON Schema and schemas embedded in OpenAPI therefore share many constructs, but do not assume that both toolchains support every extension keyword in the same way. (OAS 3.2.1 Schema Object, official OAS version list)
Checklist before adopting a schema
Have you chosen whether the schema describes a request or response boundary?
Do `$schema` and the validator support the same draft and required vocabularies?
Do `required`, `null`, rejected values, and extra-property rules match the API contract?
Do the constraints reflect actual server handling, without treating validation success as business success?
Have you checked representative invalid JSON, missing values, nulls, boundary values, and unexpected properties?
Have you checked how schema changes affect existing clients and servers before release?
References
[Getting started: create and validate a JSON Schema](https://json-schema.org/learn/getting-started-step-by-step)
[JSON Schema objects, required, and additional properties](https://json-schema.org/understanding-json-schema/reference/object)
[JSON Schema Draft 2020-12](https://json-schema.org/draft/2020-12)
[JSON Schema Validation Draft 2020-12](https://json-schema.org/draft/2020-12/json-schema-validation)
[OpenAPI Specification 3.2.1 Schema Object](https://spec.openapis.org/oas/v3.2.1.html#schema-object)