Open History to see the last commit
1---
2id: Angular-HttpClient-and-other-data-fetching-clients
3title: Angular HttpClient
4---
5β
6TanStack Query works with any data fetching client that returns a Promise, including the browser's
7`fetch` API and clients such as `graphql-request`.
8β
9## Using Angular's `HttpClient` for data fetching
10β
11Angular's `HttpClient` returns Observables, which query and mutation functions do not consume
12directly. Convert the Observable with RxJS's `firstValueFrom` or `lastValueFrom`. Query functions
13can also return a synchronous value.
14β
15```ts
16@Component({
17 // ...
18})
19class ExampleComponent {
20 private readonly http = inject(HttpClient)
21β
22 readonly repoDataQuery = injectQuery(() => ({
23 queryKey: ['repoData'],
24 queryFn: () =>
25 firstValueFrom(
26 this.http.get('https://api.github.com/repos/tanstack/query'),
27 ),
28 }))
29}
30```
31β
32For regular `HttpClient` requests, `firstValueFrom` resolves the single response and unsubscribes.
33Use `lastValueFrom` when the Observable emits multiple values and the query should resolve with the
34last one.
35β
36Angular [HTTP interceptors](https://angular.dev/guide/http/interceptors) continue to apply to
37requests made inside query and mutation functions.
38β
39## SSR
40β
41When using `HttpClient` with SSR, prefer Angular Query's built-in query hydration and disable
42Angular's HTTP transfer cache. Otherwise, both systems serialize the same request data.
43β
44Add
45[`withNoHttpTransferCache()`](https://angular.dev/api/platform-browser/withNoHttpTransferCache) to
46[`provideClientHydration()`](https://angular.dev/api/platform-browser/provideClientHydration). See
47the [SSR guide](./guides/ssr.md) for the complete setup.
48β
49For cancellation, see [Query Cancellation](./guides/query-cancellation.md).