Skip to content
Documentation
jsdoc-open-api
short
Describing Responses

Describing Responses

Describing Responses

An API specification needs to specify the responses for all API operations. Each operation must have at least one response defined, usually a successful response. A response is defined by its HTTP status code and the data returned in the response body and/or headers. Here is a minimal example:


_10
/**
_10
* GET /ping
_10
* @response 200 - OK
_10
* @responseContent {string} 200.text/plain
_10
*/

Response media types

An API can respond with various media types. JSON is the most common format for data exchange, but not the only one possible. To specify the response media types, use @responseContent.


_16
/**
_16
* GET /users
_16
* @summary Get all users
_16
* @response 200 - A list of users
_16
* @responseContent {ArrayOfUsers} 200.application/json
_16
* @responseContent {ArrayOfUsers} 200.application/json
_16
* @responseContent {string} 200.text/plain
_16
*/
_16
_16
// This operation returns image
_16
/**
_16
* GET /logo
_16
* @summary Get the logo image
_16
* @response 200 - Logo image in PNG format
_16
* @responseContent {binary} 200.image/png
_16
*/

HTTP status codes

Each response definition starts with a status code, such as 200 or 404. An operation typically returns one successful status code and one or more error statuses. To define a range of response codes, you may use the following range definitions: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response range is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code. Each response status requires a description. For example, you can describe the conditions for error responses. Markdown (CommonMark (opens in a new tab)) can be used for rich text representation.


_10
/**
_10
* @response 200 - OK
_10
* @response 400 - Bad request. User ID must be an integer and larger than 0.
_10
* @response 401 - Authorization information is missing or invalid.
_10
* @response 404 - A user with the specified ID was not found.
_10
* @response 5XX - Unexpected error.
_10
*/

An API specification does not necessarily need to cover all possible HTTP response codes, since they may not be known in advance. However, it’s expected to cover successful responses and any known errors. By "known errors" we mean, for example, a 404 Not Found response for an operation that returns a resource by ID, or a 400 Bad Request response in case of invalid operation parameters.

Response body

A response body can define:

  • An object or an array — typically used with JSON and XML APIs
  • A primitive data type such as a number or string – used for plain text responses
  • A file – (see below)

Objects can be defined in components:


_11
components:
_11
schemas:
_11
User:
_11
type: object
_11
properties:
_11
id:
_11
type: integer
_11
description: The user ID.
_11
username:
_11
type: string
_11
description: The user name.

And be used with:


_10
/**
_10
* @response 200 - A User object
_10
* @responseContent {User} 200.application/json
_10
*/

Response that returns a file

An API operation can return a file, such as an image or PDF. If the response returns the file alone, you would typically use the binary type and specify the appropriate media type for the response content:


_10
/**
_10
* GET /report
_10
* @summary Returns the report in the PDF format
_10
* @response 200 - A PDF file
_10
* @responseContent {binary} 200.application/pdf
_10
*/

Files can also be embedded into, say, JSON or XML as a base64-encoded string. In this case, you would use something like:


_10
/**
_10
* GET /users/me
_10
* @summary Returns user information
_10
* @response 200 - A JSON object containing user name and avatar
_10
* @responseContent {User} 200.application/json
_10
*/


_11
components:
_11
schemas:
_11
User:
_11
type: object
_11
properties:
_11
username:
_11
type: string
_11
avatar: # <-- image embedded into JSON
_11
type: string
_11
format: byte
_11
description: Base64-encoded contents of the avatar image

Empty response body

Responses, such as 204 No Content, have no body. To indicate the response body is empty, do not specify @responseContent:


_10
/**
_10
* @response 204 - The resource was deleted successfully.
_10
*/

Response headers

Responses from an API can include custom headers to provide additional information on the result of an API call. For example, a rate-limited API may provide the rate limit status via response headers as follows:


_10
HTTP 1/1 200 OK
_10
X-RateLimit-Limit: 100
_10
X-RateLimit-Remaining: 99
_10
X-RateLimit-Reset: 2016-10-12T11:00:00Z
_10
_10
{ ... }

You can define custom headers for each response as follows:


_10
/**
_10
* GET /ping
_10
* @summary Checks if the server is alive.
_10
* @response 200 - OK
_10
* @responseHeader {integer} 200.X-RateLimit-Limit - Request limit per hour.
_10
* @responseHeader {integer} 200.X-RateLimit-Remaining - The number of requests left for the time window.
_10
* @responseHeader {date-time} 200.X-RateLimit-Reset - The UTC date/time at which the current rate limit window resets.
_10
*/

, currently, OpenAPI Specification does not permit to define common response headers for different response codes or different API operations. You need to define the headers for each response individually.

Response examples

Examples can be defined in components:


_10
components:
_10
examples:
_10
Jessica:
_10
value:
_10
id: 10
_10
name: Jessica Smith
_10
Ron:
_10
value:
_10
id: 11
_10
name: Ron Stewart

And be used as:


_10
/**
_10
* POST /users
_10
* @summary Adds a new user
_10
* @response 200 - OK
_10
* @responseContent {User} 200.application/json
_10
* @responseExample {Jessica} 200.application/json.Jessica
_10
* @responseExample {Ron} 200.application/json.Ron
_10
*/

Default response

Sometimes, an operation can return multiple errors with different HTTP status codes, but all of them have the same response structure:


_10
/**
_10
* @response 200 - Success
_10
* @responseContent {User} 200.application/json
_10
*
_10
* @response 400 - Bad request
_10
* @responseContent {Error} 400.application/json
_10
*
_10
* @response 404 - Not found
_10
* @responseContent {Error} 404.application/json
_10
*/

You can use the default response to describe these errors collectively, not individually. "Default" means this response is used for all HTTP codes that are not covered individually for this operation.


_10
/**
_10
* @response 200 - Success
_10
* @responseContent {User} 200.application/json
_10
*
_10
* @response default - Unexpected error
_10
* @responseContent {Error} default.application/json
_10
*/

Reusing responses

Responses can be defined in components to be reused elsewhere. The following response definition:


_26
components:
_26
responses:
_26
NotFound:
_26
description: The specified resource was not found
_26
content:
_26
application/json:
_26
schema:
_26
$ref: "#/components/schemas/Error"
_26
Unauthorized:
_26
description: Unauthorized
_26
content:
_26
application/json:
_26
schema:
_26
$ref: "#/components/schemas/Error"
_26
schemas:
_26
# Schema for error response body
_26
Error:
_26
type: object
_26
properties:
_26
code:
_26
type: string
_26
message:
_26
type: string
_26
required:
_26
- code
_26
- message

Can be reused as:


_21
/**
_21
* GET /users
_21
* @summary Gets a list of users.
_21
*
_21
* @response 200 - OK
_21
* @responseContent {ArrayOfUsers} 200.application/json
_21
*
_21
* @responseComponent {Unauthorized} 401
_21
*/
_21
_21
/**
_21
* GET /users/{id}
_21
* @summary Gets a user by ID.
_21
*
_21
* @response 200 - OK
_21
* @responseContent {User} 200.application/json
_21
*
_21
* @responseComponent {Unauthorized} 401
_21
*
_21
* @responseComponent {NotFound} 404
_21
*/