In this article, we will see the steps to create a SPFx web part which calls a third-party anonymous REST API to get data from the API. Some properties like URL and item id will be passed from web part properties to change the data in the web part.

Step 1: Open your React SPFx web part and update the interface IThirdpartyApiCallProps.ts inside your components folder. Update the interface with following 3 properties and import WebPartContext to create property for web part context inside react component.

Step 2: Open web part file ThirdpartyApiCallWebPart.ts and add below 2 properties in interface IThirdpartyApiCallWebPartProps. These properties will be passed to component from the web part.

Step 3: Update render() method to set the properties of react component. Context property of react component is set from web part context. Other 2 properties are set from web part properties.

Step 4: Define 2 new text box properties for property pane of web part in getPropertyPaneConfiguration() method. These 2 properties will be passed to react component properties from web part.

Step 5: Define default value for the 2 new properties in web manifest json file. ThirdpartyApiCallWebPart.manifest.json

Step 6: Create a new .ts file in components folder with name IThirdpartyApiCallState. This interface will have all the values which we fetch from single API record All the properties of API record are stored in react component state which is defined in this interface. We will define all those properties which we want to fetch from API.

Step 6: Now open react component .tsx file ThirdpartyApiCall.tsx and do following:

1. Access state interface IThirdpartyApiCallState in tsx file as state management. Import the interface and add as state in React.Component class constructor.

2. Add construct for react component class ThirdpartyApiCall in tsx file. This constructor will be communicating with properties and state through interfaces. Here were initialing all our state properties to null.

3. Update render() method code to show values of properties received from API.

4. Add componentDidMount() method. This method is called after constructor (with null values) -> render(with printing null values) -> componentDidMount (Invoke API and Set Data Into State). Inside this method we are calling InvokeAPIAndSetDataIntoState. This is getting user details from API using  getUserDetails() method and adding to state of react component using setState() method. setState() again calls render method internally and print values received from API. Import HttpClient and HttpClientResponse.

5. When properties of web part changed, values in web part needs to be re-rendered based on the changed values. For this we need to implement componentDidUpdate() method. This method is called when properties of react component are changed. Inside this method we are calling InvokeAPIAndSetDataIntoState() again to get data from API.

Now gulp serve and check your web part with data from external API.

You can change the properties like User ID in property pane.hostforlifebanner