x-codeSamples

Usage

Use x-codeSamples (previously called x-code-samples) to add custom code samples in one or more languages to your operations. When specified, the code samples are rendered in the right panel of the API documentation. Add it to the operation OpenAPI object.

Field Name Type Description
x-codeSamples [ Code Sample Object ] A list of code samples associated with an operation.

Code Sample Object

Field Name Type Description
lang string Code sample language. Value should be one of the following list
label string Code sample label e.g. Node or Python2.7, optional, lang will be used by default
source string Code sample source code

Examples

openapi: '3.0'
info:
  ...
tags: [...]
paths:
  /example:
    get:
      summary: Example summary
      description: Example description
      operationId: examplePath
      responses: [...]
      parameters: [...]
      x-codeSamples:
        - lang: 'cURL'
          label: 'CLI'
          source: |
            curl --request POST \
            --url 'https://data.apiexample.com/api/example/batch_query/json?format=json' \
            --header 'content-type: application/octet-stream: ' \
            --data '{}'

Code sample object

YAMLJSON
lang: JavaScript
source: console.log('Hello World');
{
  "lang": "JavaScript",
  "source": "console.log('Hello World');"
}

In Reference docs

Code sample in the right panel