{"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"openapi-code-sample-tag","__idx":0},"children":["OpenAPI Code Sample Tag ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"/learn/markdoc/tags/tag-library#redocly-tag-badge"},"children":[{"$$mdtype":"Tag","name":"TagBadge","attributes":{"badgeType":"Redocly"},"children":[]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["openapi-code-sample"]}," tag renders sample code snippets directly in your documentation, showing users how to interact with API operations defined in your OpenAPI description. By default, both request and response examples are displayed."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"syntax-and-usage","__idx":1},"children":["Syntax and usage"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use the tag, pass the filepath of your OpenAPI description using the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["descriptionFile"]}," attribute and a reference to the specific operation using either ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["operationId"]}," or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pointer"]},". You can also use additional attributes to configure the code sample element rendered in the document."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["With ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["operationId"]},":"]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"title":"hello","language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"getMuseumHours","parameters":{},"environments":{}},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  operationId=\"getMuseumHours\"\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["With ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pointer"]},":"]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","pointer":"/paths/~1museum-hours/get","parameters":{},"environments":{}},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  pointer=\"/paths/~1museum-hours/get\"\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"attributes","__idx":2},"children":["Attributes"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Attribute"},"children":["Attribute"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Type"},"children":["Type"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Description"},"children":["Description"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["descriptionFile"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["REQUIRED."]}," A path to an OpenAPI description."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["operationId"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["ID of the operation inside the OpenAPI description. Required if ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pointer"]}," is not specified."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["pointer"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Pointer to an operation inside the OpenAPI description. Required if ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["operationId"]}," is not specified. Slashes used on endpoints must be encoded with ~1 -- for example, ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/paths/~1some-endpoint/post"]},"."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["exampleKey"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Key specifying which example to use. Must match the key used for an operation's example in the OpenAPI description. If it is specified, only the relevant example (request or response) is displayed."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["mimeType"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Key specifying which mimeType to use. Must match the key used for an operation's content type in the OpenAPI description."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["language"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Limits the code sample to a specific language. Works with the following values (case sensitive): Payload, curl, C#, Node.js, JavaScript, Python, R, Ruby, PHP, Go, Java."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["parameters"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Object"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Custom parameters to use for the OpenAPI operation code sample. Accepts values for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["header"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["query"]},", and predefined path parameters."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["requestBody"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Object"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Defines an inline request body to use. Replaces any request body in the code sample."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["environment"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["String"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Limits the environment picker in the code sample to a specific server defined in the OpenAPI description."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["environments"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Object"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Provide environment variables that resolve to your code sample. Variables are based on your OpenAPI description's ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["securitySchemes"]},". See ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"#environment-variables"},"children":["environment variables"]},"."]}]}]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"environment-variables","__idx":3},"children":["Environment variables"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The environment variables available in the code sample correspond with your security schemes. The name of the security scheme must be used in the variable. Here's a list of available variables:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Basic auth = ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName_username"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName_password"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["JWT token or OAuth token = ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName_token"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["OAuth credentials = ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName_client_id"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName_client_secret"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["API key auth does not have a suffix, so the variable is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["SchemeName"]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["By prefilling security credentials for logged in users, you can provide code samples that are fully ready to execute."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["For example:"]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"createSpecialEvent","parameters":{},"environments":{"https://api.fake-museum-example.com/v1":{}},"environment":"https://api.fake-museum-example.com/v1"},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  operationId=\"createSpecialEvent\"\n  environment=\"https://api.fake-museum-example.com/v1\"\n  environments={\n    \"https://api.fake-museum-example.com/v1\": {\n      \"MuseumOAuth_token\": $user-props.token,\n    }\n  }\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"examples","__idx":4},"children":["Examples"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"basic-usage","__idx":5},"children":["Basic usage"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["By default, the tag will render a code sample in multiple languages for a specific API operation, as in the following example:"]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"createSpecialEvent","parameters":{},"environments":{}},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  operationId=\"createSpecialEvent\"\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"createSpecialEvent","parameters":{},"environments":{},"codeSamplesResolved":[{"lang":"shell","title":"curl","source":"curl -i -X POST \\\n  -u '<username>:<password>' \\\n  https://api.fake-museum-example.com/v1.1/special-events \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n    \"name\": \"Mermaid Treasure Identification and Analysis\",\n    \"location\": \"Under the seaaa 🦀 🎶 🌊.\",\n    \"eventDescription\": \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n    \"dates\": [\n      \"2023-09-05\",\n      \"2023-09-08\"\n    ],\n    \"price\": 0\n  }'"},{"lang":"javascript","title":"JavaScript","source":"const resp = await fetch(\n  `https://api.fake-museum-example.com/v1.1/special-events`,\n  {\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n      Authorization: 'Basic ' + btoa('<username>:<password>')\n    },\n    body: JSON.stringify({\n      name: 'Mermaid Treasure Identification and Analysis',\n      location: 'Under the seaaa 🦀 🎶 🌊.',\n      eventDescription: 'Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.',\n      dates: [\n        '2023-09-05',\n        '2023-09-08'\n      ],\n      price: 0\n    })\n  }\n);\n\nconst data = await resp.json();\nconsole.log(data);"},{"lang":"javascript","title":"Node.js","source":"import fetch from 'node-fetch';\n\nasync function run() {\n  const resp = await fetch(\n    `https://api.fake-museum-example.com/v1.1/special-events`,\n    {\n      method: 'POST',\n      headers: {\n        'Content-Type': 'application/json',\n        Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64')\n      },\n      body: JSON.stringify({\n        name: 'Mermaid Treasure Identification and Analysis',\n        location: 'Under the seaaa 🦀 🎶 🌊.',\n        eventDescription: 'Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.',\n        dates: [\n          '2023-09-05',\n          '2023-09-08'\n        ],\n        price: 0\n      })\n    }\n  );\n\n  const data = await resp.json();\n  console.log(data);\n}\n\nrun();"},{"lang":"python","title":"Python","source":"import requests\n\nurl = \"https://api.fake-museum-example.com/v1.1/special-events\"\n\npayload = {\n  \"name\": \"Mermaid Treasure Identification and Analysis\",\n  \"location\": \"Under the seaaa 🦀 🎶 🌊.\",\n  \"eventDescription\": \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n  \"dates\": [\n    \"2023-09-05\",\n    \"2023-09-08\"\n  ],\n  \"price\": 0\n}\n\nheaders = {\"Content-Type\": \"application/json\"}\n\nresponse = requests.post(url, json=payload, headers=headers, auth=('<username>','<password>'))\n\ndata = response.json()\nprint(data)"},{"lang":"java","title":"Java","source":"import java.net.*;\nimport java.net.http.*;\nimport java.util.*;\n\npublic class App {\n  public static void main(String[] args) throws Exception {\n    var httpClient = HttpClient.newBuilder().build();\n\n    var payload = String.join(\"\\n\"\n      , \"{\"\n      , \" \\\"name\\\": \\\"Mermaid Treasure Identification and Analysis\\\",\"\n      , \" \\\"location\\\": \\\"Under the seaaa 🦀 🎶 🌊.\\\",\"\n      , \" \\\"eventDescription\\\": \\\"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\\\",\"\n      , \" \\\"dates\\\": [\"\n      , \"  \\\"2023-09-05\\\",\"\n      , \"  \\\"2023-09-08\\\"\"\n      , \" ],\"\n      , \" \\\"price\\\": 0\"\n      , \"}\"\n    );\n\n    var host = \"https://api.fake-museum-example.com\";\n    var pathname = \"/v1.1/special-events\";\n    var request = HttpRequest.newBuilder()\n      .POST(HttpRequest.BodyPublishers.ofString(payload))\n      .uri(URI.create(host + pathname ))\n      .header(\"Content-Type\", \"application/json\")\n      .header(\"Authorization\", \"Basic \" + Base64.getEncoder().encodeToString((\"<username>:<password>\").getBytes()))\n      .build();\n\n    var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());\n\n    System.out.println(response.body());\n  }\n}"},{"lang":"csharp","title":"C#","source":"using System;\nusing System.Net.Http;\nusing System.Threading.Tasks;\nusing System.Text;\nusing System.Text.Json;\nusing System.Net.Http.Headers;\n\npublic class Program\n{\n  public static async Task Main()\n  {\n    System.Net.Http.HttpClient client = new();\n\n    string base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes(\"<username>:<password>\"));\n    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(@\"Basic\", base64String);\n\n    string json = JsonSerializer.Serialize(new\n    {\n      name = \"Mermaid Treasure Identification and Analysis\",\n      location = \"Under the seaaa 🦀 🎶 🌊.\",\n      eventDescription = \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n      dates = new[] {\n        \"2023-09-05\",\n        \"2023-09-08\"\n      },\n      price = \"0\"\n    });\n\n    using StringContent postData = new(json, Encoding.UTF8, \"application/json\");\n    using HttpResponseMessage request = await client.PostAsync(\"https://api.fake-museum-example.com/v1.1/special-events\", postData);\n    string response = await request.Content.ReadAsStringAsync();\n\n    Console.WriteLine(response);\n  }\n}"},{"lang":"php","title":"PHP","source":"/**\n * Requires libcurl\n */\n\n$curl = curl_init();\n\n$payload = array(\n  \"name\" => \"Mermaid Treasure Identification and Analysis\",\n  \"location\" => \"Under the seaaa 🦀 🎶 🌊.\",\n  \"eventDescription\" => \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n  \"dates\" => array(\n    \"2023-09-05\",\n    \"2023-09-08\"\n  ),\n  \"price\" => 0\n);\n\ncurl_setopt_array($curl, [\n  CURLOPT_HTTPHEADER => [\n    \"Content-Type: application/json\",\n    \"Authorization: Basic \" . base64_encode(\"<username>:<password>\")\n  ],\n  CURLOPT_POSTFIELDS => json_encode($payload),\n  CURLOPT_URL => \"https://api.fake-museum-example.com/v1.1/special-events\",\n  CURLOPT_RETURNTRANSFER => true,\n  CURLOPT_CUSTOMREQUEST => \"POST\",\n]);\n\n$response = curl_exec($curl);\n$error = curl_error($curl);\n\ncurl_close($curl);\n\nif ($error) {\n  echo \"cURL Error #:\" . $error;\n} else {\n  echo $response;\n}"},{"lang":"go","title":"Go","source":"package main\n\nimport (\n  \"fmt\"\n  \"bytes\"\n  \"net/http\"\n  \"io/ioutil\"\n)\n\nfunc main() {\n  reqUrl := \"https://api.fake-museum-example.com/v1.1/special-events\"\n  var data = []byte(`{\n    \"name\": \"Mermaid Treasure Identification and Analysis\",\n    \"location\": \"Under the seaaa 🦀 🎶 🌊.\",\n    \"eventDescription\": \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n    \"dates\": [\n      \"2023-09-05\",\n      \"2023-09-08\"\n    ],\n    \"price\": 0\n  }`)\n  req, err := http.NewRequest(\"POST\", reqUrl, bytes.NewBuffer(data))\n  if err != nil {\n    panic(err)\n  }\n  req.SetBasicAuth(\"<username>\", \"<password>\")\n  req.Header.Add(\"Content-Type\", \"application/json\")\n  res, err := http.DefaultClient.Do(req)\n  if err != nil {\n    panic(err)\n  }\n  defer res.Body.Close()\n  body, err := ioutil.ReadAll(res.Body)\n  if err != nil {\n    panic(err)\n  }\n\n  fmt.Println(res)\n  fmt.Println(string(body))\n}"},{"lang":"ruby","title":"Ruby","source":"require 'json'\nrequire 'uri'\nrequire 'net/http'\nrequire 'openssl'\n\nurl = URI('https://api.fake-museum-example.com/v1.1/special-events')\n\nhttp = Net::HTTP.new(url.host, url.port)\nhttp.use_ssl = true\n\nrequest = Net::HTTP::Post.new(url)\nrequest['Content-Type'] = 'application/json'\nrequest.body = {\n  name: 'Mermaid Treasure Identification and Analysis',\n  location: 'Under the seaaa 🦀 🎶 🌊.',\n  eventDescription: 'Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.',\n  dates: [\n    '2023-09-05',\n    '2023-09-08'\n  ],\n  price: 0\n}.to_json\nrequest.basic_auth('<username>', '<password>')\n\nresponse = http.request(request)\nputs response.read_body\n"},{"lang":"r","title":"R","source":"library(httr)\n\nbody = '{\n   \"name\": \"Mermaid Treasure Identification and Analysis\",\n   \"location\": \"Under the seaaa 🦀 🎶 🌊.\",\n   \"eventDescription\": \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n   \"dates\": [\n    \"2023-09-05\",\n    \"2023-09-08\"\n   ],\n   \"price\": 0\n}'\n\nurl = \"https://api.fake-museum-example.com/v1.1/special-events\"\n\ndata_req <- POST(\n  url,\n  authenticate(\"<username>\", \"<password>\")\n  add_headers(\"Content-Type\" = \"application/json\"),\n  body = body,\n  encode = \"json\",\n  verbose()\n)\n\ncontent(data_req)"},{"lang":"json","title":"Payload application/json","source":"{\n  \"name\": \"Mermaid Treasure Identification and Analysis\",\n  \"location\": \"Under the seaaa 🦀 🎶 🌊.\",\n  \"eventDescription\": \"Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.\",\n  \"dates\": [\n    \"2023-09-05\",\n    \"2023-09-08\"\n  ],\n  \"price\": 0\n}"}]},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"with-single-language","__idx":6},"children":["With single language"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can restrict the code sample to a specific language using the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["language"]}," attribute. The following example limits the code sample to JavaScript:"]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"createSpecialEvent","language":"JavaScript","parameters":{},"environments":{}},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  operationId=\"createSpecialEvent\"\n  language=\"JavaScript\"\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"createSpecialEvent","language":"JavaScript","parameters":{},"environments":{},"codeSamplesResolved":[{"lang":"javascript","title":"JavaScript","source":"const resp = await fetch(\n  `https://api.fake-museum-example.com/v1.1/special-events`,\n  {\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n      Authorization: 'Basic ' + btoa('<username>:<password>')\n    },\n    body: JSON.stringify({\n      name: 'Mermaid Treasure Identification and Analysis',\n      location: 'Under the seaaa 🦀 🎶 🌊.',\n      eventDescription: 'Join us as we review and classify a rare collection of 20 thingamabobs, gadgets, gizmos, whoosits, and whatsits, kindly donated by Ariel.',\n      dates: [\n        '2023-09-05',\n        '2023-09-08'\n      ],\n      price: 0\n    })\n  }\n);\n\nconst data = await resp.json();\nconsole.log(data);"}]},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"with-pre-defined-values","__idx":7},"children":["With pre-defined values"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can use various attributes to provide values that resolve to the rendered element. The following example includes pre-defined parameters and environment variables."]},{"$$mdtype":"Tag","name":"MarkdocExample","attributes":{"renderDemo":false,"language":"markdoc","demoContent":[{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"deleteSpecialEvent","language":"curl","parameters":{"path":{"eventId":"dad4bce8-f5cb-4078-a211-995864315e39"}},"environments":{"https://api.fake-museum-example.com/v1":{"MuseumPlaceholderAuth_username":"custom-username","MuseumPlaceholderAuth_password":"custom-password"}},"environment":"https://api.fake-museum-example.com/v1"},"children":[]}],"rawContent":"{% openapi-code-sample\n  descriptionFile=\"../../../demo/openapi/museum-api.yaml\"\n  operationId=\"deleteSpecialEvent\"\n  language=\"curl\"\n  parameters={\n    path: {\n      eventId: \"dad4bce8-f5cb-4078-a211-995864315e39\"\n    }\n  }\n  environment=\"https://api.fake-museum-example.com/v1\"\n  environments={\n    \"https://api.fake-museum-example.com/v1\": {\n      \"MuseumPlaceholderAuth_username\": \"custom-username\",\n      \"MuseumPlaceholderAuth_password\": \"custom-password\"\n    }\n  }\n/%}\n"},"children":[]},{"$$mdtype":"Tag","name":"OpenApiCodeSample","attributes":{"descriptionFile":"../../../demo/openapi/museum-api.yaml","operationId":"deleteSpecialEvent","language":"curl","parameters":{"path":{"eventId":"dad4bce8-f5cb-4078-a211-995864315e39"}},"environments":{"https://api.fake-museum-example.com/v1":{"MuseumPlaceholderAuth_username":"custom-username","MuseumPlaceholderAuth_password":"custom-password"}},"environment":"https://api.fake-museum-example.com/v1","codeSamplesResolved":[{"lang":"shell","title":"curl","source":"curl -i -X DELETE \\\n  -u custom-username:custom-password \\\n  https://api.fake-museum-example.com/v1.1/special-events/dad4bce8-f5cb-4078-a211-995864315e39"}]},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"best-practices","__idx":8},"children":["Best practices"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Customize for flexibility"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The various attributes give you a lot of flexibility to customize the code sample, allowing you to meet a more diverse range of API scenarios. You can use them to tailor the code samples to the specific needs of your users."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Document code samples"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Pair code samples with comprehensive documentation. Explain the purpose of each sample, the expected behavior, and any modifications that users might need to make. This way you can help your users to successfully build with your API."]}]},"frontmatter":{},"tagList":["markdoc-example","markdoc-tag-badge","openapi-code-sample","table"],"title":"OpenAPI Code Sample Tag","lastModified":"2025-10-09T00:52:40.000Z"}