How to call an External API from SPFx webpart
- API Main URL – https://rapidapi.com/Gramzivi/api/covid-19-data
- This has 4 to 5 endpoints to get a different kind of data like, get world stats, get daily reports totals, get data by country etc…
- The endPoint which we will use – https://rapidapi.com/Gramzivi/api/covid-19-data?endpoint=apiendpoint_35f69513-ea31-48fd-a97d-4e2c8165d0b3
- This endpoint will provide us with total COVID 19 stats world data like total confirmed, recovered, critical, deaths, last change timestamp, list update timestamp.

- solution name – spfx-covid-stats
- webpart name – covidStats
- Entry component – CovidStats.tsx
Step – Passing WebPart context to React component
We would need to use webpart context to call external API using httpClient class available with this object. this class provides us method to make get and post method to any API.
Open src\webparts\controls\components\ICovidStatsProps.ts
Modify code to below
import { WebPartContext } from "@microsoft/sp-webpart-base";
export interface ICovidStatsProps {
context: WebPartContext;
}Open src\webparts\controls\CovidStatsWebPart.ts
Modify render method to pass the context.
public render(): void {
const element: React.ReactElement<ICovidStatsProps> = React.createElement(
CovidStats,
{
context: this.context
}
);
ReactDom.render(element, this.domElement);
}Please note we have just added line ‘context:this.context’
Create new file at src\ServiceProvider.ts
import { HttpClient, IHttpClientOptions, HttpClientResponse } from '@microsoft/sp-http';
import { WebPartContext } from '@microsoft/sp-webpart-base';
import { Constants } from './webparts/Constant';
export class ServiceProvider {
private wpcontext:WebPartContext;
public constructor(context: WebPartContext) {
this.wpcontext= context;
}
private httpClientOptionsForGlobal: IHttpClientOptions = {
headers: new Headers({
"x-rapidapi-host": "covid-19-data.p.rapidapi.com",
"x-rapidapi-key": "<REPLACE WHIT WITH YOUR APIKEY>"
}),
method: "GET",
mode: "cors"
};
public async getTotals() {
var response = await this.wpcontext.httpClient
.get("https://covid-19-data.p.rapidapi.com/totals", HttpClient.configurations.v1,this.httpClientOptionsForGlobal);
console.log(response);
var responeJson : any = await response.json();
return responeJson;
}
}- End Point – https://covid-19-data.p.rapidapi.com/totals
- API Key – We will get API key after subscribing to this API at https://rapidapi.com/Gramzivi/api/covid-19-data?endpoint=apiendpoint_35f69513-ea31-48fd-a97d-4e2c8165d0b3
Create new file at src\webparts\covidStats\components\Overview.tsx
import * as React from 'react';
import styles from './CovidStats.module.scss';
import { ICovidStatsProps } from './ICovidStatsProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { WebPartContext } from '@microsoft/sp-webpart-base';
import {ServiceProvider} from '../../../ServiceProvider';
export interface IOverViewProps {
context:WebPartContext;
}
export interface IOverViewState {
data:any;
}
export default class OverViewStats extends React.Component<IOverViewProps, IOverViewState> {
private serviceProvider;
public constructor(props: IOverViewProps, state: IOverViewState) {
super(props);
this.serviceProvider = new ServiceProvider(this.props.context);
this.state = {
data:{}
};
}
public render(): React.ReactElement<IOverViewProps> {
return(
<React.Fragment>
<h1>Coronavirus Cases Overview:</h1>
<h2>Confirmed : {this.state.data.confirmed}</h2>
<h2>Recovered: {this.state.data.recovered}</h2>
<h2>Critical: {this.state.data.critical}</h2>
<h2>Deaths: {this.state.data.deaths}</h2>
</React.Fragment>
);
}
public async componentDidMount(){
this.getData();
}
private getData(){
this.serviceProvider.
getTotals()
.then(
(result: any): void => {
console.log(result);
this.setState({data:result[0]});
}
)
.catch(error => {
console.log(error);
});
}
}Step – Call custom component
Open src\webparts\covidStats\components\CovidStats.tsx
Modify your code to below
import * as React from 'react';
import styles from './CovidStats.module.scss';
import { ICovidStatsProps } from './ICovidStatsProps';
import { escape } from '@microsoft/sp-lodash-subset';
import Overview from '../components/Overview';
export default class CovidStats extends React.Component<ICovidStatsProps, {}> {
public render(): React.ReactElement<ICovidStatsProps> {
return(
<React.Fragment>
<Overview context={this.props.context}>
</Overview>
</React.Fragment>
);
}
}gulp serve

- We can call any external API from SPFx web part using the inbuilt HTTP client library
- Creating custom react component
- Calling custom react component inside another component.

