Fern Definition

Packages

What is a package?

Every folder in your API definition is a package.

1
2
3
4
5
6
7
8
fern/
├─ fern.config.json
├─ generators.yml
└─ definition/ # <------ root package
  ├─ api.yml
  ├─ imdb.yml
  └─ persons/ # <-------- nested package
    ├─ director.yml

The generated SDK will match the hierarchy of your API definition.

1
2
3
4
5
6
7
const client = new Client();

// calling endpoint defined in imdb.yml
client.imdb.getRating("goodwill-hunting");

// calling endpoint defined in persons/director.yml
client.persons.director.get("christopher-nolan");

Package configuration

Each package can have a special file called __package__.yml. Just like any other definition file, it can contain imports, types, endpoints, and errors.

Endpoints in __package__.yml will appear at the root of the package. For example, the following generated SDK

1
2
3
const client = new Client();

client.submitRating("goodwill-hunting", 9.5);

would have a fern directory

1
2
3
4
5
6
fern/
├─ fern.config.json
├─ generators.yml
└─ definition/
  ├─ __package__.yml # <---
  └─ api.yml

that contains the following __package__.yml:

1
2
3
4
5
6
7
8
9
10
11
12
service:
  base-path: ""
  auth: true
  endpoints:
    submitRating:
      method: POST
      path: "/submit-rating/{movieId}"
      path-parameters:
        movieId: string
      request:
        type: double
        docs: Rating of the movie

Namespacing

Each package has its own namespace. This means you can reuse type names and error names across packages.

This is useful when versioning your APIs. For example, when you want to increment your API version, you can just copy the existing API to a new package and start making changes. If the new API version reuses certain types or errors, that's okay because the two APIs live in different packages.

1
2
3
4
5
6
7
8
9
10
fern/
├─ fern.config.json
├─ generators.yml
└─ definition/
  ├─ api.yml
  └─ imdb/
      └─ v1/
        └─ movie.yml # type names can overlap with v2/movie.yml
      └─ v2/
        └─ movie.yml

__package__.yml also allows you to configure the navigation order of your services. This is relevant when you want to control the display of your documentation.

For example, let's say you have the following Fern Directory:

1
2
3
4
5
6
7
fern/
├─ fern.config.json
├─ generators.yml
└─ definition/
  ├─ projects.yml
  ├─ roles.yml
  ├─ users.yml

Your API will be sorted alphabetically: projects, roles, then users. If you want to control the navigation, you can add a __package__.yml file and configure the order.

1
2
3
4
navigation: 
  - users.yml
  - roles.yml
  - projects.yml

Note the fern directory structure will now look like:

1
2
3
4
5
6
7
8
fern/
├─ fern.config.json
├─ generators.yml
└─ definition/
  ├─ __package__.yml # <------------ New File
  ├─ projects.yml
  ├─ roles.yml
  └─ users.yml