How to Call APIs in Angular? (2024)

Calling APIs is a major part of any web application. Today, countless APIs are waiting to be consumed in meaningful applications, whether React, Vue, Svelte or Angular-based.

Angular

Angular, led by Google, is an open-source front-end framework. Focused on building user interfaces for web and mobile applications, it is among the most popular JavaScript frameworks for building applications.

Angular is built on TypeScript and offers a powerful CLI to develop Angular applications. With that said, let's see how we can call APIs in an Angular application. I have divided the process into steps which are as follows:

→ STEP 1: Find an API

First of all, let's find an API to use in our Go application. RapidAPI Hub enables you to choose from thousands of public APIs for use in your projects.

Loading component...

For this guide, we will use the Random Facts API from RapidAPI Hub that gives us the carbon footprint data of travel.

To use this API, you need to subscribe to it first. You can do this by clicking on the Subscribe to Test button, which will redirect you to the pricing page. You can select the free plan for this guide.

After all this, you will be redirected back to the original page. Here you will have a field named x-rapidapi-key. It is your API key. Save it somewhere because we will need it in the application later.

→ STEP 2: Create the application

We need the official Angular CLI to create a new Angular application and develop it. Run the following command in your terminal to install the CLI:

sh

npm install -g @angular/cli

Once installed, we can use the command ng to access the CLI. The following command will create a new application named api-angular.

sh

ng new api-angular

Once you are done, open this project in your preferred code editor.

→ STEP 3: Start the application

To run the app, open the terminal in the api-angular directory and run the following command:

sh

This command will serve your application, an2d you will be able to view the starter app by navigating to http://localhost:4200/.

→ STEP 4: Formulate the API Request

Now, let's get our hands dirty and start writing the code to consume the API. The first thing we need to do is to import the HttpClientModule, which allows us to make HTTP requests. So, go ahead and import it in your src/app/app.module.ts file.

ts

import {NgModule} from '@angular/core';

import {BrowserModule} from '@angular/platform-browser';

import {HttpClientModule} from '@angular/common/http'; // importing the http module

import {AppRoutingModule} from './app-routing.module';

import {AppComponent} from './app.component';

@NgModule({

declarations: [AppComponent],

imports: [

BrowserModule,

AppRoutingModule,

HttpClientModule // adding it in the imports

],

providers: [],

bootstrap: [AppComponent]

})

export class AppModule {}

Now, you can add the call to the API in any component of the application. Open the app.component.ts file, and import the HttpClient and HttpHeaders modules as follows:

ts

import {Component} from '@angular/core';

import {HttpClient, HttpHeaders} from '@angular/common/http';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent {

constructor(private http: HttpClient) {}

title = 'api-angular';

}

Now, we will use the http.get method to send the request to the API. Our API call will look like this in code:

ts

let headers = new HttpHeaders({

'x-rapidapi-host': 'random-facts2.p.rapidapi.com',

'x-rapidapi-key': 'your-api-key'

});

this.http

.get<any>('https://random-facts2.p.rapidapi.com/getfact', {

headers: headers

})

.subscribe(data => {

console.log(data);

});

Notice how we added the headers, including our RapidAPI key. Replace the value of the x-rapidapi-key in the above code snippet with the API key you saved earlier.

→ FINAL STEP: The API response

Finally, we can wrap the API call in the ngOnInit() method, a lifecycle hook in Angular which is fired whenever a component finishes loading. After adding the API call in the hook, the ngOnInit hook will trigger the API call whenever the component loads. The final code looks like this:

ts

//app.component.ts

import {Component, OnInit} from '@angular/core'; // Importing OnInit hook

import {HttpClient, HttpHeaders} from '@angular/common/http';

@Component({

selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

// Implementing OnInit

constructor(private http: HttpClient) {}

title = 'api-angular';

ngOnInit() {

// API Call

let headers = new HttpHeaders({

'x-rapidapi-host': 'random-facts2.p.rapidapi.com',

'x-rapidapi-key': 'your-api-key'

});

this.http

.get<any>('https://random-facts2.p.rapidapi.com/getfact', {

headers: headers

})

.subscribe(data => {

console.log(data);

});

}

}

Now our application is ready to make API calls. Reload your application and check your console. You will see a random fact like this:

Wrap Up

This guide was an introduction to consuming APIs in Angular. We hope that now you can start using APIs in your awesome Angular projects.

How to Call APIs in Angular? (2024)
Top Articles
Swing Trading vs Options Trading: What are the Differences & Which is Right for You? - VectorVest
What Happens When Reset End-to-end Encrypted Data on iPhone [Answered]
St Thomas Usvi Craigslist
Tyler Sis 360 Louisiana Mo
Sound Of Freedom Showtimes Near Governor's Crossing Stadium 14
Napa Autocare Locator
Tesla Supercharger La Crosse Photos
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
3656 Curlew St
Craigslist Greenville Craigslist
Cvs Learnet Modules
Caliber Collision Burnsville
About Us | TQL Careers
10 Free Employee Handbook Templates in Word & ClickUp
Chastity Brainwash
Imagetrend Inc, 20855 Kensington Blvd, Lakeville, MN 55044, US - MapQuest
Voy Boards Miss America
Tygodnik Polityka - Polityka.pl
Officialmilarosee
Www.publicsurplus.com Motor Pool
Shopmonsterus Reviews
Parc Soleil Drowning
Ice Dodo Unblocked 76
Bocca Richboro
Devotion Showtimes Near Regency Buenaventura 6
Finding Safety Data Sheets
Amelia Chase Bank Murder
Divina Rapsing
Dove Cremation Services Topeka Ks
Ullu Coupon Code
Pokémon Unbound Starters
Santa Barbara Craigs List
How often should you visit your Barber?
Guide to Cost-Benefit Analysis of Investment Projects Economic appraisal tool for Cohesion Policy 2014-2020
Craigslist Cars And Trucks Mcallen
Max 80 Orl
Craigslist Albany Ny Garage Sales
Prima Healthcare Columbiana Ohio
ATM Near Me | Find The Nearest ATM Location | ATM Locator NL
Woodman's Carpentersville Gas Price
Craigslist Mexicali Cars And Trucks - By Owner
Keir Starmer looks to Italy on how to stop migrant boats
Bill Manser Net Worth
Pgecom
Craigslist Pet Phoenix
Noga Funeral Home Obituaries
Concentrix + Webhelp devient Concentrix
Aaca Not Mine
Marion City Wide Garage Sale 2023
Deviantart Rwby
Ubg98.Github.io Unblocked
Craigslist Yard Sales In Murrells Inlet
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5717

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.