Angular Consuming API
Learn how to consume API responses in Angular, store the returned data, display it in templates, and handle loading and error states.
What Does Consuming an API Mean?
Consuming an API means receiving and using the data returned by a backend API inside the Angular application.
After HttpClient sends a request, Angular receives a response that can be assigned to component properties and displayed in the template.
The response can contain a single object, an array of objects, primitive values or other structured JSON data.
API Consumption Flow
Common API Response Types
Array
A list of resources such as courses or trainers.
Object
A single resource returned by the API.
Primitive
A string, number, boolean or similar simple value.
JSON
Structured data commonly returned by REST APIs.
Using Typed API Responses
TypeScript types can be used to describe the structure of API responses.
This makes the returned data easier to work with inside Angular components and services.
export interface Course {
id: number;
name: string;
duration: string;
fees: number;
}
getCourses() {
return this.http.get<Course[]>(
this.apiUrl
);
}
Receiving API Data
courses: Course[] = [];
loadCourses() {
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
}
});
}
Received Courses
Java Full Stack
.NET Full Stack
Python Full Stack
Displaying API Data in Template
After storing the API response in a component property, the template can use Angular template syntax to display it.
A collection of objects can be rendered using Angular's current template control-flow syntax.
courses: Course[] = [];
<div>
@for (course of courses; track course.id) {
<h3>
{{ course.name }}
</h3>
<p>
Duration:
{{ course.duration }}
</p>
<p>
Fees:
₹{{ course.fees }}
</p>
}
</div>
Consuming a Single API Object
Some endpoints return one resource instead of an array.
The component can store that object in a typed property.
getCourse(id: number) {
return this.http.get<Course>(
this.apiUrl + '/' + id
);
}
course?: Course;
loadCourse(id: number) {
this.courseService
.getCourse(id)
.subscribe({
next: course => {
this.course = course;
}
});
}
Example API Response
{
"id": 101,
"name": "Java Full Stack",
"duration": "6 Months",
"fees": 35000
}
Course Details
ID: 101
Name: Java Full Stack
Duration: 6 Months
Fees: ₹35000
Handling Loading State
API requests can take time to complete. A loading flag can be used to indicate that the request is currently running.
The template can use this state to show a loading message or indicator.
courses: Course[] = [];
loading = false;
loadCourses() {
this.loading = true;
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
this.loading = false;
},
error: error => {
console.error(error);
this.loading = false;
}
});
}
@if (loading) {
<p>
Loading courses...
</p>
}
Handling Error State
When an API request fails, the application can store an error message and display it to the user.
errorMessage = '';
loadCourses() {
this.errorMessage = '';
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
this.errorMessage =
'Unable to load courses.';
}
});
}
@if (errorMessage) {
<div class="alert alert-danger">
{{ errorMessage }}
</div>
}
Handling Empty Data
An API may return an empty collection when no records match the requested condition.
The UI can provide a clear empty-state message instead of showing an empty area.
@if (courses.length === 0 && !loading) {
<p>
No courses found.
</p>
}
Transforming API Data
The API response can be transformed before being assigned to the component state.
RxJS operators such as map() can be used when a different representation is required.
this.courseService
.getCourses()
.pipe(
map(courses =>
courses.map(course => ({
...course,
displayName:
course.name +
' - ' +
course.duration
}))
)
)
.subscribe({
next: courses => {
console.log(courses);
}
});
Filtering API Data
API data can also be filtered when the UI requires only a subset of the returned records.
this.courseService
.getCourses()
.pipe(
map(courses =>
courses.filter(
course =>
course.fees >= 30000
)
)
)
.subscribe({
next: courses => {
this.courses = courses;
}
});
Filtered Courses
.NET Full Stack - ₹35000
Python Full Stack - ₹40000
Consuming Search Results
A search API can return data based on a query parameter. Angular can send the search term and use the returned collection to update the UI.
searchCourses(term: string) {
const params =
new HttpParams()
.set('search', term);
return this.http.get<Course[]>(
this.apiUrl,
{
params: params
}
);
}
search(term: string) {
this.courseService
.searchCourses(term)
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
}
Consuming Paginated API Data
Some APIs return pagination information together with the actual data.
Angular can consume both the records and pagination metadata and use them to build a paginated UI.
{
"items": [
{
"id": 1,
"name": "Java Full Stack"
},
{
"id": 2,
"name": ".NET Full Stack"
}
],
"page": 1,
"pageSize": 10,
"totalCount": 25
}
CIIT Training Institute Example
Suppose the CIIT Training Institute courses page loads course information from a backend API.
The Angular component can consume the response, store it in a typed property, and display the course information in the template.
getCourses() {
return this.http.get<Course[]>(
'/api/courses'
);
}
courses: Course[] = [];
loading = false;
errorMessage = '';
loadCourses() {
this.loading = true;
this.errorMessage = '';
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
this.loading = false;
},
error: error => {
console.error(error);
this.errorMessage =
'Unable to load courses.';
this.loading = false;
}
});
}
@if (loading) {
<p>
Loading courses...
</p>
}
@if (errorMessage) {
<div class="alert alert-danger">
{{ errorMessage }}
</div>
}
@for (course of courses; track course.id) {
<div class="course-card">
<h3>
{{ course.name }}
</h3>
<p>
Duration:
{{ course.duration }}
</p>
<p>
Fees:
₹{{ course.fees }}
</p>
</div>
}
CIIT Training Institute Courses
Java Full Stack
Duration: 6 Months
Fees: ₹35000
Course data consumed from backend API.
API Data State Flow
API Response Handling Checklist
Receive
Subscribe to the API Observable and receive the response.
Validate
Verify that the returned data matches the expected structure.
Store
Assign the data to suitable component state.
Display
Bind the component data to the Angular template.
Consuming Different Response Types
| Response | Type | Angular Property |
|---|---|---|
| Course List | Course[] | courses |
| Single Course | Course | course |
| Total Count | number | totalCount |
| Status | boolean | isActive |
| API Message | string | message |
API Consumption Best Practices
- Define TypeScript interfaces or types for structured API data.
- Keep API communication inside services.
- Maintain clear loading, success, error and empty states.
- Avoid directly duplicating API responses across multiple component properties without a clear reason.
- Transform or filter response data through RxJS pipelines when that makes the data flow clearer.
- Keep template logic simple and easy to read.
Important Points
The component can consume the response through the Observable returned by the service.
Store the received data in a suitable component state property.
Angular template syntax can display the consumed API data.
Loading, successful response, empty data and error scenarios should be considered by the UI.
Summary
Consuming an API means receiving the backend response and using that data inside the Angular application.
The response can be stored in typed component properties, transformed with RxJS operators, and displayed in Angular templates.
A good API-consuming flow is Request → Loading → Response → Store → Display, with suitable error and empty-state handling. In the next topic, we will learn about CRUD Integration.