When building modern Angular applications, one of the most repetitive tasks is creating services, injecting HttpClient, writing CRUD methods, handling responses, and repeating the same patterns across modules.
With Angularβs evolution toward standalone components and signals, the Angular team introduced a powerful new feature in Angular 18+:
π HttpResource β a declarative, type-safe, signal-powered way to consume REST APIs with almost zero boilerplate.
This blog explores what HttpResource is, why it exists, how to use it, and its advantages and limitations, with complete examples.
π‘ What is Angular HttpResource?
HttpResource is a new API inside @angular/common/http that automatically generates RESTful methods for an endpoint.
Instead of creating a full HttpClient service like this:
@Injectable({ providedIn: 'root' })
export class TodoService {
private http = inject(HttpClient);
list() {
return this.http.get<Todo[]>('/api/todos');
}
get(id: number) {
return this.http.get<Todo>(`/api/todos/${id}`);
}
create(data: Todo) {
return this.http.post('/api/todos', data);
}
}
You simply do:
export const todoResource = HttpResource({
url: '/api/todos',
});
And Angular automatically gives you:
list() β GET /todos
get(id) β GET /todos/:id
create(body) β POST /todos
update(id, body) β PUT /todos/:id
delete(id) β DELETE /todos/:id
The result is a clean, minimalist, scalable pattern for handling backend communication.
π Creating Your First HttpResource
Letβs walk through a real example.
- Define a Model
export interface Todo {
id: number;
title: string;
completed: boolean;
}
- Create the Resource
import { HttpResource } from '@angular/common/http';
export const todoResource = HttpResource({
url: '/api/todos',
});
Thatβs it.
No services.
No HttpClient boilerplate.
Just one line to create your complete API client.
- Use It Inside a Component
@Component({
selector: 'app-todo',
standalone: true,
template: `
<h2 class="title">My Todos</h2>
<div *ngFor="let t of todos">
<div class="todo-item">
{{ t.title }}
</div>
</div>
`,
})
export class TodoComponent {
todos: Todo[] = [];
constructor() {
effect(() => {
this.todos = todoResource.list().value() ?? [];
});
}
}
Why this is powerful?
resource.list() returns a signal, so whenever data refreshes, your UI updates automatically, no manual subscription or unsubscribe needed.
π― Advanced Usage: Dynamic Resources
For multi-tenant apps, environment-based URLs, or domain-based APIs, you can generate resources dynamically:
export const userResource = (tenantId: string) =>
HttpResource({
url: `/tenant/${tenantId}/users`
});
Usage:
const users = userResource('T001');
users.list().value();
This is incredibly useful for SaaS, B2B platforms, and multi-client applications.
π₯ Why HttpResource is a Game Changer
Below are the biggest advantages you get instantly when switching to HttpResource.
- π« No More Boilerplate Services
You no longer need:
@Injectable services
Injecting HttpClient
Writing separate methods for CRUD
Managing Observables manually
A single HttpResource() gives you the entire REST interface.
- π― Strong Type Safety
Because itβs fully typed with generics, TypeScript prevents incorrect payloads or response shapes.
Perfect for enterprise-grade apps.
- β‘ Signals Support Built-In
Unlike HttpClient (which gives Observables), HttpResource APIs work with signals, giving:
Auto UI updates
No subscriptions
No unsubscribe logic
Cleaner components
- π§© Flexible Composability
You can override:
headers
query params
base URL
body
behaviors
Example:
todoResource.get(1, {
queryParams: { include: 'comments' },
});
- π CRUD Support Comes Free
All CRUD methods are auto-generated, making development fast, clean, and predictable.
β οΈ Limitations of HttpResource
While HttpResource is powerful, itβs not perfect for all use cases.
- β Not Ideal for Complex Custom APIs
If your API isnβt standard REST (e.g., dozens of custom routes):
/api/user/reset-password
/api/user/verify-otp
/api/user/activity-log
Then HttpClient services may be more suitable.
- β Not Great for Heavy Business Logic
Some services need:
caching strategies
retry logic
debouncing
switchMap
conditional requests
HttpResource is intentionally simple and minimal.
- β Testing Can Be Harder
Manually mocking resources is less intuitive than mocking HttpClient.
- β Not for WebSockets or Streaming APIs
Use HttpClient or RxJS for:
live chats
stock tickers
notifications
SSE
HttpResource is strictly for REST.
π HttpResource vs HttpClient: A Quick Comparison
Feature HttpResource HttpClient
Boilerplate β Minimal β High
Type Safety β Strong β Good
Signals support β Built-in β No
CRUD generation β Automatic β Manual
Flexibility β Limited β Very flexible
Best for REST APIs Any API
In short:
Use HttpResource for clean REST APIs.
Use HttpClient when you need full control.
π When Should You Use HttpResource?
Use HttpResource when:
β Your backend follows REST conventions
β You want fast CRUD development
β You prefer signals over observables
β You want cleaner components with less code
Avoid when:
β Backend is heavily customized
β You need interceptors with business logic
β You need advanced caching or retry patterns
π Final Thoughts
Angularβs HttpResource is a huge improvement toward cleaner, more declarative, and type-safe code. It reduces service boilerplate, simplifies component logic with signals, and speeds up development significantly.
It fits perfectly into Angularβs modern philosophy:
standalone components + signals + typed APIs = cleaner, scalable apps.
If youβre building REST-heavy applications, HttpResource can drastically simplify your architecture.
Top comments (0)