Display SDK snippets

When you use Fern’s SDK Generator, you can automatically display SDK code snippets in your API Reference. These snippets appear in a language selector dropdown, with cURL as the default option.

Fern supports SDK snippets for TypeScript, Python, Ruby, Go, and .NET/C#.

SDK code snippet selector

Configuring SDK Snippets

To configure SDK snippets, you’ll need to name your SDKs in generators.yml and then reference that name in docs.yml.

1

Add examples to your API definition

In order to generate code snippets, Fern needs to read request examples from your API definition. If you’re using a Fern Definition, you can follow these instructions. If you’re using an OpenAPI Specification, you can follow these instructions.

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 Go, add repository: your-organization/your-repository to the github section.
generators.yml
1groups:
2 production:
3 generators:
4 - name: fernapi/fern-python-sdk
5 version: 4.28.4
6 output:
7 location: pypi
8 token: ${PYPI_TOKEN}
9 package-name: your-package-name # <--- add this field
10 ...
11 - name: fernapi/fern-typescript-node-sdk
12 version: 2.10.3
13 output:
14 location: npm
15 token: ${NPM_TOKEN}
16 package-name: your-package-name # <--- add this field
17 - name: fernapi/fern-ruby-sdk
18 version: 1.0.0-rc19
19 output:
20 location: rubygems
21 token: ${RUBYGEMS_TOKEN}
22 package-name: your-package-name # <--- add this field
23 - name: fernapi/fern-csharp-sdk
24 version: 2.1.15
25 output:
26 location: nuget
27 api-key: ${NUGET_API_KEY}
28 package-name: your-package-name # <--- add this field
29 - name: fernapi/fern-go-sdk
30 version: 1.8.1
31 github:
32 repository: your-organization/your-repository # <--- add this field
33 ...

SDK snippets automatically populated in your Fern Docs is a paid feature included in the SDK Basic plan.

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 Go, use the exact URL where the SDK repository is located, including the https://github.com/.
docs.yml
1navigation:
2 - api: API Reference
3 snippets:
4 python: your-package-name # <--- needs to match the naming in generators.yml
5 typescript: your-package-name # <--- needs to match the naming in generators.yml
6 ruby: your-package-name # <--- needs to match the naming in generators.yml
7 csharp: your-package-name # <--- needs to match the naming in generators.yml
8 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

As the final step, trigger your docs generation by running fern generate --docs locally or in CI/CD (i.e., GitHub Actions). The SDK snippets will now appear via a drop-down!

5

Set default snippet language

To set the default snippet language, use the default-language key at the top indentation level of docs.yml.

docs.yml
1default-language: typescript
2
3navigation:
4 - api: API Reference
5 snippets:
6 python: your-package-name
7 typescript: your-package-name

Access via API

If you’d like to bring SDK snippets into your own documentation, you can use the Snippets API. API access requires a SDK Business plan or above.

Merge.dev is an example of a Fern customer that uses the Snippets API to bring Python code samples into their API Reference.

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.