Using anonymous APIs
To consume any other type of API, such as public ones or ones that don’t use Entra ID for authentication, SPFx provides the HttpClient client. The implementation is very close to the native fetch() API provided by the browser, allowing you to completely customize the request, such as headers, body, and query parameters. There are no SPFx specificities here. Like the SharePoint-dedicated SPHttpClient client, the usage is straightforward:
- You first get a reference to
httpClientfor thethis.contextproperty:this._productCatalogService = new ProductCatalogService(this.context.httpClient);
- Then, you can make any call to your API. The following example demonstrates the usage of
HttpClientusing the Azure function we defined in the previous section, but without the authentication part:const response = await this._httpClient.get("https://demospfxfunction.azurewebsites.net/api/GetProducts ", HttpClient.configurations.v1); ...