The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119.
The Swagger specification is licensed under The Apache License, Version 2.0.
Swagger™ is a project used to describe and document RESTful APIs.
The Swagger specification defines a set of files required to describe such an API. These files can then be used by the Swagger-UI project to display the API and Swagger-Codegen to generate clients in various languages. Additional utilities can also take advantage of the resulting files, such as testing tools.
Version | Date | Notes |
---|---|---|
1.2 | 2014-03-14 | Initial release of the formal document. |
1.1 | 2012-08-22 | Release of Swagger 1.1 |
1.0 | 2011-08-10 | First release of the Swagger Specification |
A resource in Swagger is an entity that has a set of exposed operations. The entity can represent an actual object (pets, users..) or a set of logical operations collated together. It is up to the specification user to decide whether sub-resources should be referred to as part of their main resource or as a resource of their own. For example, assume the following URL set:
- /users - GET POST - /users/ - GET PATCH DELETE
In this case, there’s either one “/users” resource that contains operations on the “/users/” sub-resource, or two separate resources.
A fully qualified URL.
The files describing the RESTful API in accordance with the Swagger specification are represented as JSON objects and conform to the JSON standards.
For example, if a field is said to have an array value, the JSON array representation will be used:
"field" : [. ] >
Please note that while the API is described using JSON, the input and/or output can be in XML, YAML, plain text, or whichever format you chose to use with your API.
Unless noted otherwise, all field names in the specification are case sensitive.
The Swagger representation of the API is comprised of two file types:
In the Swagger specification, the data types are used in several locations - Operations, Operation Parameters, Models, and within the data types themselves (arrays).
The fields used to describe a given data type are added flatly to the relevant object. For example, if an object Foo has the field name , and is also a data type, then it MUST also include the field type (or its variance, as explained ahead). In this example, Foo would look like:
"Foo" : "name" : "sample", "type" : "string", . >
This section describes the general fields that are available to describe such data types. Some data types allow additional fields to extend further limitations on the data type value (see 4.3.3 Data Type Fields for further details).
Special care should be taken when referencing a model (or a complex type). There are currently two variations, and the proper variation should be documented everywhere the model may be used. This behavior will be unified in future versions of the spec.
The Swagger specification supports five data types:
Different programming languages represent primitives differently. The Swagger specification supports by name only the primitive types supported by the JSON-Schema Draft 4. However, in order to allow fine tuning a primitive definition, an additional format field MAY accompany the type primitive to give more information about the type used. If the format field is used, the respective client MUST conform to the elaborate type.
Common Name | type | format | Comments |
---|---|---|---|
integer | integer | int32 | signed 32 bits |
long | integer | int64 | signed 64 bits |
float | number | float | |
double | number | double | |
string | string | ||
byte | string | byte | |
boolean | boolean | ||
date | string | date | |
dateTime | string | date-time |
This value type is used to indicate that an operation returns no value. As such it MAY be used only for the return type of operations.
As explained above, when an object is said to include a data type, there are a set of fields it may include (some are required and some are optional).
Special care should be taken when referencing a model (or a complex type). There currently two variations, and the proper variation should be documented everywhere it may be used. This behavior will be unified in future versions of the spec.
The table below shows the available fields to describe a data type. The Validity column may impose additional restrictions as to which data type is required in order to include this field. For example, enum may only be included if the type field is set to string .
Field Name | Type | Validity | Description |
---|---|---|---|
type | string | Any | Required (if $ref is not used). The return type of the operation. The value MUST be one of the Primitves, array or a model’s id . |
$ref | string | Any | Required (if type is not used). The Model to be used. The value MUST be a model’s id . |
format | string | primitive | Fine-tuned primitive type definition. See Primitives for further information. The value MUST be one that is defined under Primitives, corresponding to the right primitive type . |
defaultValue | special | primitive | The default value to be used for the field. The value type MUST conform with the primitive’s type value. |
enum | [ string ] | string | A fixed list of possible values. If this field is used in conjunction with the defaultValue field, then the default value MUST be one of the values defined in the enum . |
minimum | string | number , integer | The minimum valid value for the type, inclusive. If this field is used in conjunction with the defaultValue field, then the default value MUST be higher than or equal to this value. The value type is string and should represent the minimum numeric value. Note: This will change to a numeric value in the future. |
maximum | string | number , integer | The maximum valid value for the type, inclusive. If this field is used in conjunction with the defaultValue field, then the default value MUST be lower than or equal to this value. The value type is string and should represent the maximum numeric value. Note: This will change to a numeric value in the future. |
items | Items Object | array | Required. The type definition of the values in the container. A container MUST NOT be nested in another container. |
uniqueItems | boolean | array | A flag to note whether the container allows duplicate values or not. If the value is set to true , then the array acts as a set. |
This object is used to describe the value types used inside an array. Of the Data Type Fields, it can include either the type and format fields OR the $ref field (when referencing a model). The rest of the listed Data Type fields are not applicable.
If the type field is included it MUST NOT have the value array . There’s currently no support for containers within containers.
For a primitive type:
"type": "string" >
For a complex type (model):
"$ref": "Pet" >
The File (case sensitive) is a special type used to denote file upload. Note that declaring a model with the name File may lead to various conflicts with third party tools and SHOULD be avoided.
When using File , the consumes field MUST be "multipart/form-data" , and the paramType MUST be "form" .
The Resource Listing serves as the root document for the API description. It contains general information about the API and an inventory of the available resources.
By default, this document SHOULD be served at the /api-docs path.
Field Name | Type | Description |
---|---|---|
swaggerVersion | string | Required. Specifies the Swagger Specification version being used. It can be used by the Swagger UI and other clients to interpret the API listing. The value MUST be an existing Swagger specification version. Currently, "1.0" , "1.1" , "1.2" are valid values. The field is a string type for possible non-numeric versions in the future (for example, “1.2a”). |
apis | [ Resource Object ] | Required. Lists the resources to be described by this specification implementation. The array can have 0 or more elements. |
apiVersion | string | Provides the version of the application API (not to be confused by the specification version). |
info | Info Object | Provides metadata about the API. The metadata can be used by the clients if needed, and can be presented in the Swagger-UI for convenience. |
authorizations | Authorizations Object | Provides information about the authorization schemes allowed on this API. |
"apiVersion": "1.0.0", "swaggerVersion": "1.2", "apis": [ "path": "/pet", "description": "Operations about pets" >, "path": "/user", "description": "Operations about user" >, "path": "/store", "description": "Operations about store" > ], "authorizations": "oauth2": "type": "oauth2", "scopes": [ "scope": "email", "description": "Access to your email address" >, "scope": "pets", "description": "Access to your pets" > ], "grantTypes": "implicit": "loginEndpoint": "url": "http://petstore.swagger.wordnik.com/oauth/dialog" >, "tokenName": "access_token" >, "authorization_code": "tokenRequestEndpoint": "url": "http://petstore.swagger.wordnik.com/oauth/requestToken", "clientIdName": "client_id", "clientSecretName": "client_secret" >, "tokenEndpoint": "url": "http://petstore.swagger.wordnik.com/oauth/token", "tokenName": "access_code" > > > > >, "info": "title": "Swagger Sample App", "description": "This is a sample server Petstore server. You can find out more about Swagger \n at \"http://swagger.wordnik.com\">http://swagger.wordnik.com or on irc.freenode.net, #swagger. For this sample,\n you can use the api key \"special-key\" to test the authorization filters", "termsOfServiceUrl": "http://helloreverb.com/terms/", "contact": "apiteam@wordnik.com", "license": "Apache 2.0", "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html" > >
The Resource object describes a resource API endpoint in the application.
Field Name | Type | Description |
---|---|---|
path | string | Required. A relative path to the API declaration from the path used to retrieve this Resource Listing. This path does not necessarily have to correspond to the URL which actually serves this resource in the API but rather where the resource listing itself is served. The value SHOULD be in a relative (URL) path format. |
description | string | Recommended. A short description of the resource. |
"path": "/pets", "description": "Operations about pets." >
The API Declaration provides information about an API exposed on a resource. There should be one file per Resource described. The file MUST be served in the URL described by the path field.
Field Name | Type | Description |
---|---|---|
swaggerVersion | string | Required. Specifies the Swagger Specification version being used. It can be used by the Swagger UI and other clients to interpret the API listing. The value MUST be an existing Swagger specification version. Currently, "1.0" , "1.1" , "1.2" are valid values. |
apiVersion | string | Provides the version of the application API (not to be confused by the specification version). |
basePath | string | Required. The root URL serving the API. This field is important because while it is common to have the Resource Listing and API Declarations on the server providing the APIs themselves, it is not a requirement. The API specifications can be served using static files and not generated by the API server itself, so the URL for serving the API cannot always be derived from the URL serving the API specification. The value SHOULD be in the format of a URL. |
resourcePath | string | The relative path to the resource, from the basePath , which this API Specification describes. The value MUST precede with a forward slash ( "/" ). |
apis | [API Object] | Required. A list of the APIs exposed on this resource. There MUST NOT be more than one API Object per path in the array. |
models | Models Object | A list of the models available to this resource. Note that these need to be exposed separately for each API Declaration. |
produces | [ string ] | A list of MIME types the APIs on this resource can produce. This is global to all APIs but can be overridden on specific API calls. |
consumes | [ string ] | A list of MIME types the APIs on this resource can consume. This is global to all APIs but can be overridden on specific API calls. |
authorizations | Authorizations Object | A list of authorizations schemes required for the operations listed in this API declaration. Individual operations may override this setting. If there are multiple authorization schemes described here, it means they’re all applied. |
"apiVersion": "1.0.0", "swaggerVersion": "1.2", "basePath": "http://petstore.swagger.wordnik.com/api", "resourcePath": "/store", "produces": [ "application/json" ], "authorizations": <>, "apis": [ "path": "/store/order/ ", "operations": [ "method": "GET", "summary": "Find purchase order by ID", "notes": "For valid response try integer IDs with value ", "type": "Order", "nickname": "getOrderById", "authorizations": <>, "parameters": [ "name": "orderId", "description": "ID of pet that needs to be fetched", "required": true, "type": "string", "paramType": "path" > ], "responseMessages": [ "code": 400, "message": "Invalid ID supplied" >, "code": 404, "message": "Order not found" > ] >, "method": "DELETE", "summary": "Delete purchase order by ID", "notes": "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", "type": "void", "nickname": "deleteOrder", "authorizations": "oauth2": [ "scope": "test:anything", "description": "anything" > ] >, "parameters": [ "name": "orderId", "description": "ID of the order that needs to be deleted", "required": true, "type": "string", "paramType": "path" > ], "responseMessages": [ "code": 400, "message": "Invalid ID supplied" >, "code": 404, "message": "Order not found" > ] > ] >, "path": "/store/order", "operations": [ "method": "POST", "summary": "Place an order for a pet", "notes": "", "type": "void", "nickname": "placeOrder", "authorizations": "oauth2": [ "scope": "test:anything", "description": "anything" > ] >, "parameters": [ "name": "body", "description": "order placed for purchasing the pet", "required": true, "type": "Order", "paramType": "body" > ], "responseMessages": [ "code": 400, "message": "Invalid order" > ] > ] > ], "models": "Order": "id": "Order", "properties": "id": "type": "integer", "format": "int64" >, "petId": "type": "integer", "format": "int64" >, "quantity": "type": "integer", "format": "int32" >, "status": "type": "string", "description": "Order Status", "enum": [ "placed", " approved", " delivered" ] >, "shipDate": "type": "string", "format": "date-time" > > > > >
The API Object describes one or more operations on a single path . In the apis array, there MUST be only one API Object per path .
Field Name | Type | Description |
---|---|---|
path | string | Required. The relative path to the operation, from the basePath , which this operation describes. The value SHOULD be in a relative (URL) path format. |
description | string | Recommended. A short description of the resource. |
operations | [Operation Object] | Required. A list of the API operations available on this path. The array may include 0 or more operations. There MUST NOT be more than one Operation Object per method in the array. |
"path": "/pet", "operations": [ "method": "PUT", "summary": "Update an existing pet", "notes": "", "type": "void", "nickname": "updatePet", "authorizations": <>, "parameters": [ "name": "body", "description": "Pet object that needs to be updated in the store", "required": true, "type": "Pet", "paramType": "body" > ], "responseMessages": [ "code": 400, "message": "Invalid ID supplied" >, "code": 404, "message": "Pet not found" >, "code": 405, "message": "Validation exception" > ] >, "method": "POST", "summary": "Add a new pet to the store", "notes": "", "type": "void", "nickname": "addPet", "consumes": [ "application/json", "application/xml" ], "authorizations": "oauth2": [ "scope": "test:anything", "description": "anything" > ] >, "parameters": [ "name": "body", "description": "Pet object that needs to be added to the store", "required": true, "type": "Pet", "paramType": "body" > ], "responseMessages": [ "code": 405, "message": "Invalid input" > ] > ] >
The Operation Object describes a single operation on a path .
In the operations array, there MUST be only one Operation Object per method .
This object includes the Data Type Fields in order to describe the return value of the operation. The type field MUST be used to link to other models.
This is the only object where the type MAY have the value of void to indicate that the operation returns no value.
Field Name | Type | Description |
---|---|---|
method | string | Required. The HTTP method required to invoke this operation. The value MUST be one of the following values: "GET" , "HEAD" , "POST" , "PUT" , "PATCH" , "DELETE" , "OPTIONS" . The values MUST be in uppercase. |
summary | string | A short summary of what the operation does. For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters. |
notes | string | A verbose explanation of the operation behavior. |
nickname | string | Required. A unique id for the operation that can be used by tools reading the output for further and easier manipulation. For example, Swagger-Codegen will use the nickname as the method name of the operation in the client it generates. The value MUST be alphanumeric and may include underscores. Whitespace characters are not allowed. |
authorizations | Authorizations Object | A list of authorizations required to execute this operation. While not mandatory, if used, it overrides the value given at the API Declaration’s authorizations. In order to completely remove API Declaration’s authorizations completely, an empty object ( <> ) may be applied. |
parameters | [Parameter Object] | Required. The inputs to the operation. If no parameters are needed, an empty array MUST be included. |
responseMessages | [Response Message Object] | Lists the possible response statuses that can return from the operation. |
produces | [ string ] | A list of MIME types this operation can produce. This is overrides the global produces definition at the root of the API Declaration. Each string value SHOULD represent a MIME type. |
consumes | [ string ] | A list of MIME types this operation can consume. This is overrides the global consumes definition at the root of the API Declaration. Each string value SHOULD represent a MIME type. |
deprecated | string | Declares this operation to be deprecated. Usage of the declared operation should be refrained. Valid value MUST be either "true" or "false" . Note: This field will change to type boolean in the future. |
"method": "GET", "summary": "Find pet by ID", "notes": "Returns a pet based on ID", "type": "Pet", "nickname": "getPetById", "authorizations": <>, "parameters": [ "name": "petId", "description": "ID of pet that needs to be fetched", "required": true, "type": "integer", "format": "int64", "paramType": "path", "minimum": "1.0", "maximum": "100000.0" > ], "responseMessages": [ "code": 400, "message": "Invalid ID supplied" >, "code": 404, "message": "Pet not found" > ] >
The Parameter Object describes a single parameter to be sent in an operation and maps to the parameters field in the Operation Object.
This object includes the Data Type Fields in order to describe the type of this parameter. The type field MUST be used to link to other models.
If type is File , the consumes field MUST be "multipart/form-data" , and the paramType MUST be "form" .
"name": "id"