Skip to content

Commit 310dbd1

Browse files
authored
(api definitions) Add auth.yml example and fix client credentials syntax for Fern Definition (#895)
1 parent 600efb7 commit 310dbd1

File tree

1 file changed

+45
-6
lines changed
  • fern/products/api-def/ferndef-pages

1 file changed

+45
-6
lines changed

fern/products/api-def/ferndef-pages/auth.mdx

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ client = new Client({
178178

179179
## OAuth client credentials
180180

181-
If your API uses OAuth, you can specify an oauth scheme. Note that you'll need to define a token retrieval endpoint.
181+
If your API uses OAuth, you can specify an oauth scheme in `api.yml` and define a token retrieval endpoint in a separate `auth.yml` file ([example](https://github.com/fern-api/fern/blob/3137938b70e058f3691ddef34d5c1cc29acc4b80/test-definitions/fern/apis/oauth-client-credentials/definition/api.yml)).
182182

183183
```yaml api.yml
184184
name: api
@@ -194,15 +194,54 @@ auth-schemes:
194194
client-id-env: YOUR_CLIENT_ID
195195
client-secret-env: YOUR_CLIENT_SECRET
196196
get-token:
197-
endpoint: auth.getToken
197+
endpoint: auth.getTokenWithClientCredentials
198+
request-properties:
199+
client-id: $request.client_id # Format: parameter-name: $request.property_name
200+
client-secret: $request.client_secret # Format: parameter-name: $request.property_name
198201
response-properties:
199-
access-token: $response.access_token
200-
expires-in: $response.expires_in
202+
access-token: $response.access_token # Format: parameter-name: $response.property_name
203+
expires-in: $response.expires_in # Format: parameter-name: $response.property_name
201204
202205
```
203206

204-
If the `expires-in` property is set, the generated OAuth token provider will automatically refresh the token when it expires.
205-
Otherwise, it's assumed that the access token is valid indefinitely.
207+
The `request-properties` and `response-properties` map OAuth standard parameters to your actual endpoint's request and response field names defined in `auth.yml`.
208+
209+
<Info>
210+
If the `expires-in` property is set, the generated OAuth token provider will automatically refresh the token when it expires. Otherwise, it's assumed that the access token is valid indefinitely.
211+
</Info>
212+
213+
The corresponding `auth.yml` file ([example](https://github.com/fern-api/fern/blob/3137938b70e058f3691ddef34d5c1cc29acc4b80/test-definitions/fern/apis/oauth-client-credentials/definition/auth.yml)) defines the token endpoint:
214+
215+
```yaml title="auth.yml"
216+
types:
217+
TokenResponse:
218+
docs: |
219+
An OAuth token response.
220+
properties:
221+
access_token: string
222+
expires_in: integer
223+
refresh_token: optional<string>
224+
225+
service:
226+
auth: false
227+
base-path: /
228+
endpoints:
229+
getTokenWithClientCredentials:
230+
path: /token
231+
method: POST
232+
request:
233+
name: GetTokenRequest
234+
body:
235+
properties:
236+
client_id: string
237+
client_secret: string
238+
audience: literal<"https://api.example.com">
239+
grant_type: literal<"client_credentials">
240+
scope: optional<string>
241+
response: TokenResponse
242+
```
243+
244+
<Info>If your OAuth server is hosted at a different URL than your main API, you can use [multi-URL environments](/api-definitions/ferndef/api-yml/environments#multiple-urls-per-environment) to specify separate base URLs for authentication and API calls.</Info>
206245

207246
With this, all of the OAuth logic happens automatically in the generated SDKs. As long as you configure these settings, your
208247
client will automatically retrieve an access token and refresh it as needed.

0 commit comments

Comments
 (0)