Firstly create your specification: object

from json import load
from openapi_core import create_spec

with open('openapi.json', 'r') as spec_file:
   spec_dict = load(spec_file)

spec = create_spec(spec_dict)


Now you can use it to validate against requests

from openapi_core.validation.request.validators import RequestValidator

validator = RequestValidator(spec)
result = validator.validate(request)

# raise errors if request invalid

# get list of errors
errors = result.errors

and unmarshal request data from validation result

# get parameters object with path, query, cookies and headers parameters
validated_params = result.parameters
# or specific location parameters
validated_path_params = result.parameters.path

# get body
validated_body = result.body

# get security data
validated_security =

Request object should be instance of OpenAPIRequest class (See Integrations).


You can also validate against responses

from openapi_core.validation.response.validators import ResponseValidator

validator = ResponseValidator(spec)
result = validator.validate(request, response)

# raise errors if response invalid

# get list of errors
errors = result.errors

and unmarshal response data from validation result

# get headers
validated_headers = result.headers

# get data
validated_data =

Response object should be instance of OpenAPIResponse class (See Integrations).


openapi-core supports security for authentication and authorization process. Security data for security schemas are accessible from security attribute of RequestValidationResult object.

For given security specification:

  - BasicAuth: []
  - ApiKeyAuth: []
      type: http
      scheme: basic
      type: apiKey
      in: header
      name: X-API-Key

you can access your security data the following:

result = validator.validate(request)

# get basic auth decoded credentials['BasicAuth']

# get api key['ApiKeyAuth']

Supported security types:

  • http – for Basic and Bearer HTTP authentications schemes

  • apiKey – for API keys and cookie authentication