REST Clients
We all send HTTP requests in a slightly different way. Hey API doesn't force you to use any specific technology. What we do, however, is support your choice with great clients. All seamlessly integrated with our other features.
Next.js and Axios coming soon.
Features
- seamless integration with
@hey-api/openapi-ts
- typesafe response data and errors
- access to the original request and response
- each request call is configurable
- minimal learning curve thanks to extending the underlying technology
Fetch API
WARNING
Fetch API client is currently in beta. The interface might change before it becomes stable. We encourage you to leave feedback on GitHub.
Start by adding @hey-api/client-fetch
into your project's dependencies.
npm install @hey-api/client-fetch
pnpm add @hey-api/client-fetch
yarn add @hey-api/client-fetch
bun add @hey-api/client-fetch
Ensure you have already installed and configured @hey-api/openapi-ts
. Update your configuration to use the client package.
export default {
client: '@hey-api/client-fetch',
input: 'path/to/openapi.json',
output: 'src/client',
}
You can now run openapi-ts
as usual to generate the new services.
Configuration
You will most likely want to configure the global client instance used by services. You can do that with the createClient()
method. Call it at the beginning of your program.
import { createClient } from '@hey-api/client-fetch';
createClient({
baseUrl: 'https://example.com',
});
Interceptors
Another common requirement is request authorization. Interceptors are ideal for adding headers to your requests.
import { client } from '@hey-api/client-fetch';
client.interceptors.request.use((request, options) => {
request.headers.set('Authorization', 'Bearer <my_token>');
return request;
});
Example
You can view a more complete example on this page.
Legacy Clients
Before standalone client packages, clients were generated using @hey-api/openapi-ts
. If you want to generate a legacy client that isn't published as a standalone package, you can use the client
config option.
export default {
client: 'angular',
input: 'path/to/openapi.json',
output: 'src/client',
}
export default {
client: 'node',
input: 'path/to/openapi.json',
output: 'src/client',
}
export default {
client: 'xhr',
input: 'path/to/openapi.json',
output: 'src/client',
}
The following legacy clients are available:
- angular (using RxJS)
- node (using node-fetch)
- xhr
If you'd like a standalone package for your client, let us know by opening an issue.
Examples
You can view live examples on StackBlitz.