{"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"generate-code-samples-automatically","__idx":0},"children":["Generate code samples automatically"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Redocly can automatically generate code samples in supported languages based on your API definition. You can control the display of optional properties and parameters, and hide request payload samples."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"/docs-legacy/api-reference-docs/resources/code-samples-languages"},"children":["list of supported languages"]}," indicates currently supported version(s) for each language."]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"warning"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Auto-generated code samples are not available in Redoc (the \"community edition\")."]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"prerequisites","__idx":1},"children":["Prerequisites"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Valid API definition file(s) with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["requestBody"]}," objects"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The content of auto-generated code samples is affected by the API definition in the following ways:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If your API definition has several security schemes defined as alternatives in your ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["Security Scheme"]}," object, only the first one is included in auto-generated code samples. If the security schemes are defined as mandatory in every request, they are all included."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If your API definition lists several servers in the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Server"]}," object (for example, ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["default"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["development"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["production"]},"...), only the first listed server is included in auto-generated code samples."]}]}]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info","name":"Note"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Custom code samples directly added to the API definition using the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["x-codeSamples"]}," specification extension have precedence over auto-generated ones."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["For instance, if your API definition already contains a JavaScript sample, and you enable auto-generated JavaScript samples, your docs only show the custom sample from the API definition."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Redocly Workflows user account and/or access to configuration files for Redocly Developer portal and API docs"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To enable auto-generated code samples, you must specify in which languages to generate them. The configuration procedure depends on the Redocly product you're using."]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If you manage your API definitions and documentation with Workflows, refer to the ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"#workflows-configuration"},"children":["Workflows"]}," section."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If you're using the Redocly configuration file with Workflows or building API documentation on-premise, use the ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"#api-docs-configuration"},"children":["API docs"]}," instructions."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To include auto-generated code samples in your developer portal, refer to the ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"#developer-portal-configuration"},"children":["Developer portal"]}," section."]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"configure-code-samples","__idx":2},"children":["Configure code samples"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"workflows-configuration","__idx":3},"children":["Workflows configuration"]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Log into Workflows and select the API version for which you want to configure auto-generated code samples."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["From the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Overview"]}," page, navigate to ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Settings > Features"]},"."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["On the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Features"]}," page, expand the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Generate code samples"]}," section."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["To enable code samples, select the desired language(s) from the list."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Select ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Save"]}," to apply changes. Saving your changes triggers a new build."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Change order of languages"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can change the order of languages by dragging them up or down in the list. This order affects the order of tabs (from left to right) in the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Request samples"]}," section of your docs."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Custom labels for code sample tabs"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["By default, the names of selected languages are used as the tab captions in the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Request samples"]}," section of your docs. To update it, select the pencil icon to the right of the language name, and set a custom label for the code sample tab."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Control appearance of generated code samples"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To further control the appearance of generated code samples, you can enable the following settings as required:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Skip optional properties in auto-generated payload samples"]}," - When selected, only required fields are included in auto-generated code samples and in request payload samples."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Do not show request payload tab"]}," - When selected, the code sample for the request payload is not displayed in the ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["Request samples"]}," section of your docs."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Skip optional parameters"]}," - When selected, optional parameters ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["cookies"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["headers"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["query params"]}," are not included in generated code samples."]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"developer-portal-configuration","__idx":4},"children":["Developer portal configuration"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Your Developer portal can ",{"$$mdtype":"Tag","name":"Link","attributes":{"href":"/docs-legacy/developer-portal/guides/reference-docs-integration"},"children":["integrate API docs"]}," and contain API documentation for one or multiple API definitions."]},{"$$mdtype":"Tag","name":"ol","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To enable auto-generated code samples, you must modify the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[".page.yaml"]}," configuration file for each of the definitions that should have the code samples."]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["In the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[".page.yaml"]}," configuration file, find or create the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["settings"]}," object and add the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["generateCodeSamples"]}," settings like in the following example:"]}]}]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"yaml","header":{"controls":{"copy":{}}},"source":"type: reference-docs\ndefinitionId: acme\nsettings:\ngenerateCodeSamples:\n  languages:\n    - lang: JavaScript\n      label: JS\n    - lang: C#\n    - lang: Java\n    - lang: Go\n","lang":"yaml"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Using this particular example enables generating code samples for JavaScript, C#, Java and Go."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"api-docs-configuration","__idx":5},"children":["API docs configuration"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To enable auto-generated code samples for API docs, you must modify the Redocly configuration file."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following options refer to auto-generated code samples. They correspond to the options available in Workflows, and can be used in the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[".page.yaml"]}," configuration file(s) for Developer portal."]},{"$$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":"Option"},"children":["Option"]},{"$$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":["generateCodeSamples"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["The object that controls the options for auto-generating code samples. ",{"$$mdtype":"Tag","name":"br","attributes":{},"children":[]}," ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["Note that custom code samples directly added to the API definition using the x-codeSamples specification extension have precedence over auto-generated ones."]}]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["generateCodeSamples.languages"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Array of language config objects; indicates in which languages to generate code samples."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["generateCodeSamples.languages.lang"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Can be one of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["curl"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["C#"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["JavaScript"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Java"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Java+Apache"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Go"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Node.js"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["PHP"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Python"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["R"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Ruby"]},"."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["generateCodeSamples.languages.label"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Optional label for the generated code sample. Can be any string, e.g. ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["JS"]}," or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Awesome Language"]},". When configured here, the label is displayed instead of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["lang"]}," as the tab caption in the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Request samples"]}," section of your docs."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["generateCodeSamples.skipOptionalParameters"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["When enabled, optional parameters ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["cookies"]},", ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["headers"]}," and ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["query params"]}," are not included in generated code samples. The default value is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["false"]},"."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["onlyRequiredInSamples"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Skip optional properties in auto-generated payload samples. It also affects generated code samples. The default value is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["false"]},"."]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["hideRequestPayloadSample"]}]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Do not show request ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Payload"]}," example. The default value is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["false"]},"."]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following excerpt from the Redocly configuration file illustrates how to enable auto-generated code samples for several languages, add a custom label to one of them, and hide the request payload tab from the API documentation."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"yaml","header":{"controls":{"copy":{}}},"source":"theme:\n  openapi:\n    htmlTemplate: ./docs/index.html\n    generateCodeSamples:\n      languages:\n        - lang: curl\n          label: Custom label\n        - lang: Python\n        - lang: JavaScript\n    hideRequestPayloadSample: true\n","lang":"yaml"},"children":[]}]},"frontmatter":{"excludeFromSearch":true,"seo":{"title":"Generate code samples automatically"}},"tagList":["admonition","html"],"title":"Generate code samples automatically","lastModified":"2025-08-10T02:58:02.000Z"}