Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

X++ call to external API

Posted on by 339
Hello:
We have a software vendor running their application on one of our VM. Their application exposes a set of API's which I need to call from within F&O. I've not done this before and was looking for some guidance. The initial questions rattling in my head is how would I authenticate and build the GET request? Would I need to create an AOT service or can this all be done within X++ code? BTW, I've done some light testing with Postman for the specific API call I need. Any help (with as much detail) would be greatly appreciated. 
Thank you. 
Categories:
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    Hi Martin,
    So sorry. Got sidetracked. I was able to get an mini-POC working to retrieve the token and make a simple GET call to our vendor's API. Thank you very much for your help.
     
    If I could ask for one more favor, would you mark your response as an answer please? I haven't been able to for a while. Thanks again. 
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    Thank you Anthony. I scoured around but wasn't able to find the specific example. 
  • Anthony Blake Profile Picture
    Anthony Blake 2,168 Super User 2024 Season 2 on at
    X++ call to external API
    Quick note to add that if you want to find some example code & patterns in the application, the exchange rate providers call external APIs.
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    That makes perfect sense. Within Postman, I'm retrieving the bearer token before making request calls. Thank you. 
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 229,928 Most Valuable Professional on at
    X++ call to external API
    How you should deal with authentication/authorization depends on what the web service is using. The vendor should be able to tell you how to use their API; I can only make some assumptions.
     
    You mentioned an error message regarding a bearer token, therefore it seems to be using some kind of bearer authentication. And there is a good chance that it's OAuth2. Please confirm.
     
    You'll need two HTTP calls. One to get the token (and you must have permissions to get it) and then you'll use this token in the Authentication header when calling the actual web service. For example: httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "the token").
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    Hi Martin,
     
    What I haven't mentioned yet is the initial use case for this integration. Simply, there will be a reference value entered within a F&O form when the packing slip is generated. I will use this value to call the vendor API to get further details to then populate our custom fields. I imagine this is a pretty common use case.
     
    You're right, I would prefer not to make it more complicated than necessary. However, I feel there will be additional integration to this application in the future which would benefit from doing this initial implementation 'properly'. That said, I do not have the experience with C# and would appreciate if you're able to share resources on how to get started with creating such libraries. 
     
    I think to get my feet wet, I will start with the HttpClient. I have seen examples posted online, though it was not clear to me how authorization was attained. My main (only) reference is Postman at this point. I was hoping you could briefly speak to this topic. 
     
    Thank you.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 229,928 Most Valuable Professional on at
    X++ call to external API
    It indeed sounds like RESTful (or REST-like) API. Swagger is a way how to document such web services, generate code consuming them and so on.
     
    If the API is publicly available, then you can it directly. Another question is whether you want to do it (e.g. you may want to decouple the systems by having a message queue in between), but I guess you don't want to complicate it more than necessary.
     
    Calling it directly from X++ is possible. You'll still use .NET classes such as HttpClient, but you don't necessarily have to write any C# code. On the other hand, you can make the whole work much easier by creating a C# library, especially if you generate all the code from the Swagger definition. Even if you didn't have Swagger, you could still use features like Paste JSON as classes. Therefore I usually create a C# library, unless it's something trivial that doesn't need things like data contract classes.
     
    You can put something like d365fo HttpClient to a search engine to find blog posts showing HTTP calls from X++. I opened a few and none of them had code that I would recommend, but it'll work as a starting point.
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    Hi Martin,
     
    The vendor's API was built with Swagger (RESTful?). The .NET libraries you referenced, will they be used directly within X++ code? If I need to write C# code, then that's another hurdle for me. 
     
    To your question of accessibility from the internet, I'm not sure, to be honest. Because the application is running on our VM, I've been able to use the server's public IP to access the API from Postman on my personal laptop (no VPN connection required). In fact, I'm able to hit the URL I use in Postman with my Chrome browser, and I get an "invalid bearer token" error message back. It appears the API is accessible from the internet. 
     
    Thank you!
  • CU29041349-0 Profile Picture
    CU29041349-0 339 on at
    X++ call to external API
    Hi Layan,
     
    Unfortunately, I do not have any code at this point for review. This is an area I've not had any experience with. 
     
    Thank you. 
  • Martin Dráb Profile Picture
    Martin Dráb 229,928 Most Valuable Professional on at
    X++ call to external API
    How to use their API depends on how they implemented it, but you seem to already know that (if you're able to call it from Postman). The mention of Postman and GET shows that you're talking about a some kind of web service (and not other types of application programming interfaces).
     
    Let's discuss how to consume the web service after you give us more information about it (e.g. whether its a RESTful service, SOAP or so). In general, you'll typically use .NET libraries to handle communication.
     
    You won't define a service in AOT; that would be useful if you wanted to create a web service in F&O that other systems will call.
     
    Another important information is whether the web service is accessible from Internet (such as Azure data centers running F&O and whether it should be). If the answers are yes, you can directly call it from F&O, if not, you'll need a kind of reverse proxy. The proxy can also help you with authentication.
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans