What's the best way to use an external api?

Toriyama 231 Reputation points
2022-06-05T23:44:18.003+00:00

I've been using ajax jquery, but I'm not sure if it's the best.
I've found it a little slow, but I don't know if it interferes that much at this point. At least this already works.

Another way would be with wpcontext.httpClient. I've been saying some problems trying to use this in OnInit before rendering, but it seems like a good solution. I need the render to only run when my OnInit has already gotten all the values I need from the api.

And I still know the axios.

I have been using a project without JavaScript Framework.

Thanks

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,380 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 16,901 Reputation points Microsoft Vendor
    2022-06-06T09:21:49.257+00:00

    Hi @Toriyama ,
    Per my research, We can use a web part context to call an external API using httpClient class available with this object. This class provides us with a method to make get and post methods to any API. Here is the steps for us to reference
    https://www.c-sharpcorner.com/article/how-to-call-an-external-api-from-a-spfx-webpart/

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Rob Windsor 1,781 Reputation points
    2022-06-06T15:11:37.807+00:00

    The Microsoft Learn module entitled Use Microsoft Graph & third-party APIs shows you to use native SharePoint Framework types to consume external APIs in your projects. SharePoint Framework projects can incorporate data from anonymous APIs, those REST APIs secured with Azure AD, and Microsoft Graph.

    • Calling anonymous APIs using HttpClient
    • Calling Azure AD secured APIs using AadHttpClient
    • Calling Microsoft Graph using MSGraphClient