Angular API Integration
Learn how Angular applications connect with backend APIs, send requests, receive responses and work with application data.
What is API Integration?
API Integration means connecting an Angular application with a backend service so that the application can send and receive data.
Angular applications commonly communicate with REST APIs using HttpClient.
API integration allows the frontend to work with real application data instead of depending only on static values.
Angular API Communication Flow
Why Use APIs?
Dynamic Data
Load data from a backend instead of hard-coded values.
Centralized Data
Frontend and backend can work with the same application data.
Real Applications
Build applications that communicate with databases and backend services.
Separation
Frontend presentation and backend business logic can remain separate.
What is a REST API?
A REST API exposes backend resources through HTTP endpoints that clients can communicate with.
Angular sends an HTTP request to an endpoint and receives a response from the backend service.
GET /api/courses
Angular HttpClient
Angular provides HttpClient for making HTTP requests to backend services.
HttpClient methods return Observable-based results that can be consumed by Angular components and services.
import {
HttpClient
} from '@angular/common/http';
API Integration Through a Service
API communication is commonly placed inside an Angular service instead of directly writing all HTTP logic inside the component.
This keeps components focused on presentation and application interaction while the service handles communication.
import { Injectable } from '@angular/core';
import {
HttpClient
} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class CourseService {
private apiUrl =
'/api/courses';
constructor(
private http: HttpClient
) {
}
getCourses() {
return this.http.get<Course[]>(
this.apiUrl
);
}
}
Consuming API Data in Component
export class CoursesComponent {
courses: Course[] = [];
constructor(
private courseService: CourseService
) {
}
loadCourses() {
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
}
}
Complete API Request Flow
Common HTTP Methods
GET
Retrieve data from the backend.
POST
Send data to create a new resource.
PUT
Update an existing resource.
DELETE
Remove a resource from the backend.
GET Request
A GET request is used to retrieve data from an API endpoint.
getCourses() {
return this.http.get<Course[]>(
'/api/courses'
);
}
POST Request
POST is commonly used when the frontend needs to send data to the backend to create a new resource.
addCourse(course: Course) {
return this.http.post<Course>(
'/api/courses',
course
);
}
PUT Request
PUT can be used to send updated data for an existing resource.
updateCourse(
id: number,
course: Course
) {
return this.http.put<Course>(
'/api/courses/' + id,
course
);
}
DELETE Request
DELETE can be used to remove a resource through an API endpoint.
deleteCourse(id: number) {
return this.http.delete(
'/api/courses/' + id
);
}
Working with API Responses
The backend commonly sends structured data such as JSON.
Angular can assign the received response to component properties and use it to update the user interface.
[
{
"id": 1,
"name": "Java Full Stack"
},
{
"id": 2,
"name": ".NET Full Stack"
}
]
API Error Handling
API communication can fail because of network problems, invalid requests, server errors or other backend conditions.
Angular applications should handle failed requests and provide an appropriate user experience.
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(
'API Error:',
error
);
}
});
Loading State
API requests may take time to complete. A component can maintain a loading state while waiting for the response.
This allows the interface to show the user that data is currently being loaded.
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;
}
});
}
CIIT Training Institute Example
Suppose the CIIT Training Institute website displays the available training courses from a backend API.
The Angular component calls a service, the service sends an HTTP request, and the API returns the course data.
@Injectable({
providedIn: 'root'
})
export class CourseService {
private apiUrl =
'/api/courses';
constructor(
private http: HttpClient
) {
}
getCourses() {
return this.http.get<Course[]>(
this.apiUrl
);
}
}
courses: Course[] = [];
loadCourses() {
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
}
CIIT Training Institute Courses
Java Full Stack
.NET Full Stack
Python Full Stack
Course data loaded from API.
Frontend and Backend Architecture
API Request Overview
| HTTP Method | Purpose | HttpClient Method |
|---|---|---|
| GET | Retrieve data |
http.get()
|
| POST | Create data |
http.post()
|
| PUT | Update data |
http.put()
|
| DELETE | Delete data |
http.delete()
|
API Integration Steps
Create Service
Create an Angular service for API communication.
Inject HttpClient
Use HttpClient to communicate with the backend.
Call API
Use GET, POST, PUT or DELETE according to the requirement.
Handle Response
Subscribe to the result and update the application state.
Important Points
Services provide a central place for backend communication.
HttpClient provides Angular APIs for making HTTP requests.
Process successful responses and handle failures appropriately.
A clear Component → Service → API flow makes applications easier to maintain.
API Integration Best Practices
- Keep HTTP communication inside dedicated services where practical.
- Use meaningful API endpoint and service method names.
- Handle loading, success and error states appropriately.
- Define suitable TypeScript types for API responses.
- Avoid duplicating the same API communication logic in multiple components.
- Keep components focused on UI and application interaction.
Summary
API integration connects an Angular frontend with a backend service so that the application can work with dynamic data.
Angular commonly uses HttpClient for HTTP communication, while services can organize API-related logic.
The basic flow is Component → Service → HttpClient → REST API → Response . In the next topic, we will learn about Connecting API.