Skip to content

Commit 41868e8

Browse files
authored
Revamp Customize Method Names page (#79)
1 parent 0cd582c commit 41868e8

File tree

1 file changed

+104
-17
lines changed

1 file changed

+104
-17
lines changed

fern/products/sdks/guides/customize-method-names.mdx

Lines changed: 104 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,109 @@ title: Customize Method Names
33
description: Fine-tune SDK resources and method names
44
---
55

6-
Fern allows you to fine-tune your SDK method and group names so that
7-
your SDK reads exactly how you want it to. For example, instead of
8-
`client.postUsers` you can configure the SDK to read `client.users.create()`.
6+
You can fine-tune your SDK method and group names to create intuitive, user-friendly
7+
code that matches your API's purpose.
8+
9+
For example, instead of `client.postUsers`
10+
you can configure your SDK to read `client.users.create()`.
11+
12+
This page covers how to customize method and group names using Fern Definition and OpenAPI specs.
13+
14+
<Tabs>
15+
<Tab title="Fern Definition">
16+
17+
## Configure custom method names in `api.yml`
18+
19+
For a Fern Definition, the method name comes from the endpoint directly.
20+
You can specify group and method names directly within your service structure.
21+
22+
In the example below, Fern will generate a method called `client.users.create()`:
23+
24+
```yaml title="api.yaml" {4, 6}
25+
services:
26+
http:
27+
UsersService: # specify the group
28+
base-path: /users # HTTP path
29+
endpoints:
30+
create: # configure SDK method name
31+
method: POST
32+
path: ""
33+
```
34+
35+
</Tab>
36+
<Tab title="OpenAPI">
37+
38+
## Configure custom method names in `openapi.yaml`
39+
40+
You can customize your SDK method names in two ways:
41+
42+
* **Let Fern parse `operationId` (automatic):** By default, Fern uses your
43+
operation ID to generate method names. Format your operation IDs like
44+
`{tag_name}_{operation_name}` (e.g., `users_get`) and Fern will automatically
45+
generate `users.get()`. If your operation ID doesn't start with a tag, Fern
46+
uses it directly as the method name.
47+
48+
* **Use the `x-fern-sdk-group-name` and `x-fern-sdk-method-name` extensions
49+
(manual)** to explicitly define your method name and grouping.
50+
51+
To manually configure your method name, use the `x-fern-sdk-group-name` and
52+
`x-fern-sdk-method-name` extensions. In the example below, Fern will generate a
53+
method called `client.users.create()` for the `POST /users` endpoint.
54+
55+
```yaml title="openapi.yml" {4-5}
56+
paths:
57+
/users:
58+
post:
59+
x-fern-sdk-group-name: users
60+
x-fern-sdk-method-name: create
61+
```
62+
63+
## Top level methods
64+
65+
If you omit the `x-fern-sdk-group-name` extension, then the generated SDK method
66+
will live at the root of the client rather than nested under a resource group.
67+
In the example below, Fern will generate a method called `client.send()`:
68+
69+
```yaml title="openapi.yaml" {4}
70+
paths:
71+
/send:
72+
post:
73+
x-fern-sdk-method-name: send
74+
```
75+
76+
## Multiple levels of nesting
77+
78+
<Note>
79+
See how merge.dev uses nested groups [here](https://github.com/merge-api/merge-node-client?tab=readme-ov-file#create-link-token).
80+
</Note>
81+
82+
If you add more than one `x-fern-sdk-group-name` extension, then the generated SDK will nest group names.
83+
The order of the group names is preserved in the generated SDK method.
84+
85+
In the example below, Fern will generate a method called `client.users.notifications.send()`:
86+
87+
```yaml title="openapi.yaml"
88+
paths:
89+
/users/notifications:
90+
post:
91+
x-fern-sdk-group-name:
92+
- users
93+
- notifications
94+
x-fern-sdk-method-name: send
95+
```
96+
</Tab>
97+
</Tabs>
98+
99+
## Casing is automatically configured
100+
101+
Fern automatically handles choosing the appropriate casing for each SDK
102+
language: `snake_case` in python, `camelCase` in TypeScript and `PascalCase` in
103+
Go, etc. Because of this, you define the endpoint structure once and get
104+
properly formatted methods across all generated SDKs.
105+
106+
## Generated SDK examples
107+
108+
Here's how developers using your generated SDK would call the customized method names in their applications:
9109

10110
<CodeBlocks>
11111
<CodeBlock title="TypeScript">
@@ -32,8 +132,7 @@ your SDK reads exactly how you want it to. For example, instead of
32132
</CodeBlock>
33133
</CodeBlocks>
34134

35-
Groups can also be arbitrarily nested. For example, if you want to nest the `users`
36-
endpoints under an `admin` group, the SDK would then read:
135+
Here's how users would call nested groups:
37136

38137
<CodeBlocks>
39138
<CodeBlock title="TypeScript">
@@ -59,15 +158,3 @@ endpoints under an `admin` group, the SDK would then read:
59158
```
60159
</CodeBlock>
61160
</CodeBlocks>
62-
63-
<Note>
64-
See how merge.dev uses nested groups [here](https://github.com/merge-api/merge-node-client?tab=readme-ov-file#create-link-token).
65-
</Note>
66-
67-
If you're using an OpenAPI Specification, you'll need to leverage the [`x-fern-sdk-method-name`](/learn/api-definition/openapi/extensions#sdk-method-names)
68-
extension. If you're using the fern definition, then the method name comes from the endpoint directly.
69-
70-
## Casing
71-
72-
Additionally, Fern handles choosing the appropriate casing for each SDK
73-
language: `snake_case` in python, `camelCase` in TypeScript and `PascalCase` in Go, etc.

0 commit comments

Comments
 (0)