Skip to Content

Configure Headers for HTTP-based APIs

Published: 2022-05-23

WunderGraph supports multiple HTTP-based API styles, including REST through OpenAPI Specification, GraphQL and GraphQL Federation.

As all of them are HTTP-based protocols, they have something in common, Headers. Headers allow us to forward to send meta information about a request to an HTTP server. The server can read the Header before reading the body to make decisions or read the body in a specific way. One popular use case for Headers is to use it to send authentication information.

WunderGraph supports two modes for sending headers to an origin. The "static" mode allows you to embed a static Header into all requests. This is useful e.g. for an API key.

The second mode is "clientRequestHeader", which means that we forward a Header from the client request to the origin. This is useful, e.g. when the user obtained a personal API key which is now used to authenticate them against the origin. In this case, we can simply forward the Header.

We're using GraphQL as an example but the configuration applies to all HTTP-based APIs.

const spaceX = introspect.graphql({
apiNamespace: "spacex",
url: "https://api.spacex.land/graphql/",
headers: builder => builder
// this static header will be send to all origin requests
.addStaticHeader("StaticToken","secret")
// this clientRequestHeader configuration takes the Header value from the request and injects it into the upstream request
// client -> Authorization -> origin Authorization
// you could also rename the Header key for the origin
.addClientRequestHeader("Authorization","Authorization")
});

Product

Comparisons

Subscribe to our newsletter!

Stay informed when great things happen! Get the latest news about APIs, GraphQL and more straight into your mailbox.

© 2022 WunderGraph