Security and Authentication in API
In OpenAPI, the security object is used to specify the security requirements for an API. The security object can be defined at the global level for the entire API or at the operation level for specific operations. It allows you to declare the authentication methods and authorization scopes required to access certain parts of your API.
Here are the key components of the OpenAPI security options:
Global Security:
- Define security requirements at the global level, applying to all operations in the API. This is typically done at the top level of the OpenAPI document.
Code-based configuration​
security:
- apiKey: []
paths:
# ...Visual configuration​

In this example, the API uses an API key for authentication. The
securityarray specifies the security scheme (e.g.,apiKey) and any required scopes.Operation-Level Security:
Define security requirements at the operation level, providing a way to specify different authentication methods for different operations.
paths:
/users:
get:
summary: Get a list of users
security:
- apiKey: []
post:
summary: Create a new user
security:
- oauth2: ['write']
In this example, the
GET /usersoperation requires an API key (apiKey), while thePOST /usersoperation requires OAuth 2.0 with thewritescope.Security Schemes:
Use the
securitySchemesobject within thecomponentssection to define the authentication methods that can be referenced in thesecurityobject.components:
securitySchemes:
apiKey:
type: apiKey
in: header
name: Authorization
oauth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://example.com/oauth/authorize
tokenUrl: https://example.com/oauth/token
scopes:
read: Read access
write: Write access
In this example, two security schemes (
apiKeyandoauth2) are defined in thecomponents/securitySchemessection.Authentication Types:
- OpenAPI supports various authentication types, including API keys, OAuth 2.0, HTTP basic authentication, and more. The
typeproperty within each security scheme specifies the authentication type.
- OpenAPI supports various authentication types, including API keys, OAuth 2.0, HTTP basic authentication, and more. The
Scopes:
If using OAuth 2.0 or a similar scheme that supports scopes, you can define the available scopes and specify which scopes are required for specific operations.
components:
securitySchemes:
oauth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://example.com/oauth/authorize
tokenUrl: https://example.com/oauth/token
scopes:
read: Read access
write: Write access
In this example, the
oauth2scheme includes two scopes:readandwrite.
By using the security options, you can clearly define the authentication methods and authorization requirements for your API. This information is crucial for developers who want to consume your API, as it helps them understand how to authenticate themselves and access different parts of the API securely.
Reference: https://swagger.io/docs/specification/authentication/