How to Get the Client IP Address in an Angular Application

It’s very simple to have our client address in an Angular application. With so many open sources, API addresses directly return the client’s IP address as a client request from its own browser to the API.
In this article, I’ll use this API for the Client IP Address in JSON.
First, you have to create an Angular Application using Angular CLI.
Step 1
Add a new angular service in your app using CLI with the following command:
"ng g service IpService"
After this command, you must have two new files in your Angular app source.
How To Get The Client IP Address In Angular Application
Go to Folder explorer and check for new files.
How To Get The Client IP Address In Angular Application
Step 2
Now open app.module.ts file and import HttpClientModule from “@angular/common/http” so that you can work with http:// services and can call API.
  1. import { HttpClientModule } from "@angular/common/http";  
  3. imports: [  
  4.     ...,HttpClientModule  
  5.   ]   
Open your Service File and use the following code:
  1. import { Injectable } from '@angular/core';  
  2. import { HttpClient  } from '@angular/common/http';  
  4. @Injectable({  
  5.   providedIn: 'root'  
  6. })  
  7. export class IpServiceService  {  
  9.   constructor(private http:HttpClient) { }  
  10.   public getIPAddress()  
  11.   {  
  12.     return this.http.get("");  
  13.   }  
  14. }  
Here in this code, first we imported HttpClient from @angular/common/http and injected a dependency in service constructor. At last, we created a function named getIPAddress() from where we’ll return an Observable from here.
Step 3
Now open your component file as I did in my app.component.ts. Firstly, import and inject the service object in the component’s constructor and subscribe the getIpAddress() function from your service into a local variable, as in the following code:
  1. import { Component, OnInit } from '@angular/core';  
  2. import { IpServiceService } from './ip-service.service';  
  4. @Component({  
  5.   selector: 'app-root',  
  6.   templateUrl: './app.component.html',  
  7.   styleUrls: ['./app.component.css']  
  8. })  
  9. export class AppComponent implements OnInit {  
  10.   constructor(private ip:IpServiceService){}  
  11.   title = 'DemoApp';  
  12.   ipAddress:string;  
  13. ngOnInit()  
  14.   {  
  15.     this.getIP();  
  16.   }  
  17.   getIP()  
  18.   {  
  19.     this.ip.getIPAddress().subscribe((res:any)=>{  
  20.       this.ipAddress=res.ip;  
  21.     });  
  22.   }  
  23. }  
Finally, open your app.component.html and try to print that variable using interpolation brackets.
  1. <div class="container"> <p>Your IP Address : {{ipAddress}}</p> </div>   
Step 4
Serve your angular app and open in your browser to check the IP Address, so open terminal in Visual Studio code and write the command:
Ng serve –open
You might get the output as shown below:
How To Get The Client IP Address In Angular Application
Here is the client IP Address, hope you enjoyed it!