How To Call API In Angular
How To Call API And Get Data In Angular (PART-1)
How To Call API And Get Data In Angular (PART-2

In This Blog, you are going to learn how to call API and how to get data from an API and display it in a view File in our Angular App. Calling API in our applications is a must need sometimes.


  • What is an API ?
  • How API Works In Angular ?
  • Understand HTTPClientModule
  • Create Service
  • Make a call to API
  • Display Data From API
What Is API ?
What Is API ?

What is an API ?

According to Wikipedia’s Definition of API: In computer programming, (API) is a set of subroutine definitions (functions), protocols, data and tools for building software and applications.

Workflow of API In Angular
Workflow of API In Angular
Getting API Data In Angular

Getting API Data In Angular

  • Every App must important to sending API request to another server. we can use http client request and get data and store data information to our server.
  • Specially when you are working with angular, vue, react application. you must have to learn how to run http client request with angular.
  • You can use this example with angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 version.

1st Step: Create New App

You can easily create your angular app using bellow command:

ng new my-new-app

2nd Step: Import HttpClientModule

  • In this step, we need to import HttpClientModule to app.module.ts file. so let’s import it as like bellow:
    • src/app/app.module.ts

3rd Step: Create Service for API

Here, we need to create service for http client request. we will create service file and write client http request code. this service will use in our component file.

ng g s services/post

4th Step: Use Service to Component

  • Now we have to use this services to our app component.
    • src/app/app.component.ts

5th Step: Updated View File

  • Now here, we will updated our html file. let’s put bellow code:
    • src/app/app.component.html

Source Code

app.module.ts File Code – Importing HTTP Client Module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {HttpClientModule} from '@angular/common/http';

  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

post.service.ts File Code – Service File For API

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';

  providedIn: 'root'
export class PostService {
private url = '';
  constructor(private client:HttpClient) { }

    return this.client.get(this.url);

app.component.ts File – Using Service in this app component

import { Component } from '@angular/core';
import { PostService } from './services/post.service';

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  title = 'prj-api';

constructor(private post:PostService) {}

ngOnInit(){ => {
  this.posts = response;


app.component.html File – Display API Data In HTML File

<h1>Getting Data From An API</h1>
  <li *ngFor="let p of posts">
    {{ p.title }} -- {{}}

Download Notes Of This Blog, From The Link Given Below.

Download Source Code Of This Blog, From The Link Given Below.

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *