web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to connect with a API endpoint

(0) ShareShare
ReportReport
Posted on by 172

Hi,

I would like get some guidance on creating a API endpoint.

We need to connect with a 3rd party created API endpoint to exchange a standarized Invoice information in 2-way system.

The API is created with OAUTH2 authentication and need cliendid and secred. There is a SWAGGER documentation that I cant even read or use properly...

It has GET / POST method for exchanging data and status info, and DEL for messages.

I don't know where to start with it... Can I go here using X++ .NET? 

How to create a connection to this endpoint? It need a async connection, and both sides need to call each other.

I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    What exactly do you when you say that you need an asynchronous connection? If you make a direct call from X++ from the web service endpoint, it's a synchronous connection. Maybe you're fine with a synchronous connection, you just want to execute it in an asynchronous way (e.g. by a batch).

    If you want truly asychronous solution, you can't the endpoint from X++. You need to put a message to a storage (e.g. a message queue) and have something (e.g. a logic app) that can pick the message later and calls the web service.

    Note that if you make direct web service calls, both systems must be running at the same time and see each other. If, for example, the other systems tries to call F&O while this is down for maintenance, the call will fail. Either the message is lost of the other system must have its own queue somewhere and try to send the message again later. If it puts the message to, say, a Service Bus queue, the messsage will stay there until processed.

  • RadekM Profile Picture
    172 on at

    I assumed it must be asynchromus based on it's documentation. I'm not 100% sure how it looks like.

  • RadekM Profile Picture
    172 on at

    As I'm trying the first step here... I have 3 problems.  First one always the WebRequest.Create() gives _message Error Null reference, whatever API i put in the URI.

    Secondary, I don't really know how to put the parameters into it. As I have below I need to pass 2 parameters.

    This rhing is propably the consexuence of the two  since the GetResponse() gives me error 400 or 500 depending on API URI.

    Any suggestions?

    //SWAGGER
    
    {
      "contextIdentifier": {
        "type": "onip",
        "identifier": "1111111111"
      }
    }

    class EInvoiceApiConnect
    {
    
    
        public static void Main(Args _args)
        {
            System.Net.WebRequest webreq = System.Net.WebRequest::Create("https://ksef-test.mf.gov.pl/api/online/Session/AuthorisationChallenge");
    
            System.IO.Stream streamstr,responsestr;
    
            System.IO.StreamWriter streamWriter;
    
            System.Net.WebResponse webresponse;
    
            System.IO.StreamReader reader;
    
    
    
            webreq.set_Method("POST");
    
    
            webreq.set_ContentType('application/json');
    
            streamstr = webreq.GetRequestStream();
    
            streamWriter = new System.IO.StreamWriter(streamstr);
    
            streamWriter.Write("IdentifierType=onip&identifier=11111111");
    
            streamWriter.Flush();
    
            streamWriter.Close();
    
            streamWriter.Dispose();
    
            webresponse = webreq.GetResponse();
    
            responsestr = webresponse.GetResponseStream();
    
            reader = new System.IO.StreamReader(responsestr);
    
            info(reader.ReadToEnd());
        }
    
    }

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Are you saying that calling

    System.Net.WebRequest::Create("https://ksef-test.mf.gov.pl/api/online/Session/AuthorisationChallenge");

    throw an exception with a message "_message Error Null reference"? Is it the exact message? What's the type of the exception?

    Regarding parameters, I see you're putting something to streamWriter.Write(), but I would expect a JSON object instead of URL-encoded values.

  • RadekM Profile Picture
    172 on at

    The returned values:

    ChallengedUri:

    "Object reference not set to an instance of an object."

    ResponseStatusCode:

    "Object reference not set to an instance of an object."

    pastedimage1643963368374v1.png

    As for parameters I couldn't find any working sample code..

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    You screenshot suggests that the call didn't throw an exception at all. It seems that you're looking at properties of an object constructed by Create(), therefore it must have succeeded.

    You're looking at messages that you see in debugger when looking at non-initialized properties and you incorrectly believe that it's the same thing as an unhandled exception thrown when calling Create().

  • RadekM Profile Picture
    172 on at

    Yes, the Create() doesn't throw a exception, as you said I was concern about these errors in the method.

    The only exception is thrown in GetResponse() as it gives me error code 400 or 500 based on the API URI.

    Could you please provide me some sample on how to pass a parameter using JSON? So I could get a hang on it?

  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    JSON is just a string and you already know how to add a string. You just need to know what JSON you want to send then do it. For example, if you wanted the JSON string from your first code block, you could do this:

    streamWriter.Write('{"contextIdentifier": {"type": "onip", "identifier": "1111111111" }}');

    Of course, you could use a JSON serializer instead of constructing JSON string by yourself.

    Another problem is in how you're disposing resources. You aren't disposing all resources and you don't dispose anything in case of an exception. Let me rewrite your code a little bit.

    using System.IO;
    using System.Net;
    ...
    
    WebRequest webReq = WebRequest::Create("https://ksef-test.mf.gov.pl/api/online/Session/AuthorisationChallenge");
    webReq.Method = 'POST';
    webReq.ContentType = 'application/json';
    
    using (Stream requestStream = webReq.GetRequestStream())
    {
    	using (StreamWriter streamWriter = new StreamWriter(requestStream))
    	{
    		streamWriter.Write("...");
    
    		using (WebResponse webResponse = webReq.GetResponse())
    		{
    			using (Stream responseStr = webResponse.GetResponseStream())
    			{
    				using (StreamReader reader = new StreamReader(responseStr))
    				{
    					info(reader.ReadToEnd());
    				}
    			}
    		}
    	}
    }

    (I wrote this code here, not in an IDE, so forgive me if there are mistakes.)

  • RadekM Profile Picture
    172 on at

    Thank you!

    As my old code got now a positive response the rewriten code gives me back the response 400 code / bad request.

    Don't I need the crearing methods of streamWriter()?

    streamWriter.Write('{"contextIdentifier": {"type": "onip", "identifier": "1111111111" }}');
    
                    using (WebResponse webResponse = webReq.GetResponse())

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    What do you mean by crearing methods of streamWriter()?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans