fern-api / fern

Input OpenAPI. Output SDKs and Docs.
https://buildwithfern.com
MIT License
2.45k stars 118 forks source link

[Feature] Custom headers on API requests in fern spec #2582

Open gregorybchris opened 5 months ago

gregorybchris commented 5 months ago

Problem description

It would be great to be able to send arbitrary headers on requests from the SDK to the API. Fern currently supports the headers X-Fern-SDK-Language and X-Fern-SDK-Version, but if a Fern user wants to send a different header on all requests from their SDK that's not currently possible. This feature request is to provide some configuration option in the fern spec to send an arbitrary header.

Why would it be useful?

Client information is very useful to maintain in server-side telemetry. Allowing the configuration of arbitrary headers in the fern spec helps in scenarios when server code is not easily accessible. This can make the Fern onboarding process a lot easier as the user can update a single line in the Fern config and conform to the API's expectations for header metadata without updating the API.

More details

This may be included in the generators.tml under generator config. Perhaps something like:

config:
  headers:
    X-My-Own-Header: "my-own-header-value"
    X-My-Other-Header: "my-other-header-value"

Though I don't have enough experience with the Fern spec to know if there's a better place/format for this configuration.

armandobelardo commented 5 months ago

Hi there @gregorybchris is something like this what you have in mind?: https://docs.buildwithfern.com/api-definition/fern-definition/api-yml-reference#global-headers

gregorybchris commented 5 months ago

Hey @armandobelardo! If I'm reading this correctly, this is specifying which headers the API can accept on requests, right?

Whereas I'm more talking about configuring the SDK to send a specific header and value to the API on every request. It would be more similar to the existing X-Fern-SDK-Language and X-Fern-SDK-Version headers which are sent on every request, except configurable by the Fern user.

armandobelardo commented 3 months ago

@gregorybchris this should do what you're asking for if you assign the header to a literal! If you're leveraging an OpenAPI spec, the config would be based on: https://docs.buildwithfern.com/api-definition/openapi/open-api-extensions#global-headers

x-fern-global-headers:
  - header: X-My-Own-Header
    name: myOwnHeader
    type: literal<"my-own-header-value">