Skip to navigation

Display SDK snippets

View as Markdown

If you use Fern to generate SDKs, you can display SDK code snippets in your API Reference. These snippets show examples using your actual SDK in TypeScript, Python, Go, and other supported languages.

Once configured, SDK snippets replace HTTP snippets.

Dynamic snippets

By default, SDK snippets are dynamic code examples that allow users to modify parameters and see code examples update in real time across all supported languages.

Alternatively, you can disable dynamic snippets in your docs.yml and use static code examples.

SDK code snippet selector

Configuration

To configure SDK snippets, first name your SDKs in generators.yml and then reference that name in docs.yml.

1

Add examples to your API definition

Fern needs to read request examples from your API definition to generate code snippets. For OpenAPI, follow Swagger’s examples documentation.

2

Define a package name for your SDK(s)

Configure package names in your generators.yml file:

  • For Python, TypeScript, Ruby, and .NET/C#, add package-name: your-package-name to the output section.
  • For Java, add coordinate: com.your-org:your-package-name to the output section.
  • For PHP, add packageName: YourPackageName to the config section.
  • For Go, add repository: your-organization/your-repository to the github section.
Fern supports SDK snippets for TypeScript, Python, Ruby, Go, Java, PHP, and .NET/C#. File an issue to request additional languages.
generators.yml
groups:
production:
generators:
- name: fern-python-sdk
version: 5.30.2
output:
location: pypi
token: ${PYPI_TOKEN}
package-name: your-package-name # <--- add this field
...
- name: fern-typescript-sdk
version: 3.94.0
output:
location: npm
token: ${NPM_TOKEN}
package-name: your-package-name # <--- add this field
- name: fern-ruby-sdk
version: 1.24.1
output:
location: rubygems
token: ${RUBYGEMS_TOKEN}
package-name: your-package-name # <--- add this field
- name: fern-csharp-sdk
version: 2.83.3
output:
location: nuget
api-key: ${NUGET_API_KEY}
package-name: your-package-name # <--- add this field
...
- name: fern-java-sdk
version: 4.19.2
output:
location: maven
coordinate: com.your-org:your-package-name # <--- add this field
...
- name: fern-php-sdk
version: 2.20.4
github:
repository: your-organization/your-repository
config:
packageName: YourPackageName # <--- add this field
...
- name: fern-go-sdk
version: 1.58.1
github:
repository: your-organization/your-repository # <--- add this field
...
3

Add the package name to your docs configuration

Add the package name for the corresponding SDK to your docs.yml file:

  • For Python, TypeScript, Ruby, and .NET/C#, your-package-name must match the your-package-name that you configured in your generators.yml file.
  • For Java, com.your-org:your-package-name must match the coordinate that you configured in your generators.yml file.
  • For PHP, YourPackageName must match the packageName that you configured in your generators.yml file.
  • For Go, use the exact URL where the SDK repository is located, including the https://github.com/.
docs.yml
navigation:
- api: API Reference
snippets:
python: your-package-name # <--- needs to match the naming in generators.yml
typescript: your-package-name # <--- needs to match the naming in generators.yml
ruby: your-package-name # <--- needs to match the naming in generators.yml
csharp: your-package-name # <--- needs to match the naming in generators.yml
java: com.your-org:your-package-name # <--- needs to match the coordinate in generators.yml
php: YourPackageName # <--- needs to match the packageName in generators.yml
go: https://github.com/your-organization/your-repository # <--- needs the https://github.com/ prefix

To display different package names for SDK users versus documentation users, use overrides files.

4

Trigger generation

Trigger your docs generation by running fern generate --docs locally or in CI/CD (i.e., GitHub Actions). The SDK snippets will appear via a dropdown!

Additional options

Specify SDK versions

You can specify which SDK version to use when generating code snippets.

docs.yml
navigation:
- api: API Reference
snippets:
python:
package: your-package-name # <--- needs to match the naming in generators.yml
version: your-version number # SDK version to use for snippets

Set default snippet language

Use the default-language key at the top indentation level of docs.yml. This setting applies to both SDK snippets and HTTP snippets.

docs.yml
default-language: typescript
navigation:
- api: API Reference
snippets:
python: your-package-name
typescript: your-package-name

Endpoint request and response snippets

Looking for information on generating API endpoint request and response snippets? See our documentation on Endpoint Request Snippets and Endpoint Response Snippets.