Connect to an API

Hello there,
I am trying to connect to an API. when I try to connect I get the following result

01 02

The question is, how can I pass these values using the Server API action? Thank you!

Hi Nikolaos,

That would depend on the requirements of the api you are using. Some will require URL parameters, others will want a json body, others still will want you to post parameters, etc. Best to start with the docs from the api which will have an explanation and samples.

–Ken

Hi @mebeingken thank you for your reply.
A sample of their code is something like below

/*
    Following are the variables required to connect with WebAPI.
    You need to modify them accordingly. Yu  don;t need to change the token variable, since it will be updated by the login call.

    -- Explain Variables --
    webapihost: the http(s) address of the WbAPI server
    scrollerid: The scroller ID (used as an example in our case)
    commandid: The automation ID (used as an example in our case)
    token: The authentination Web API token returned by the call to the Login API
*/


var webapihost = "http://localhost/webapi2/";
var scrollerid = "ESWBCat/WEBAPI_Order2"; // For our example, the View (scroller) ID that will be called
var commandid = "WEBAPI_order2";                   // For our example, the Automation Command ID that will be called
var mastertableid = "ESFIDocumentTrade";
var token = "";

/***********  DEMO  ************/

$(document).ready(function () {

    /*
          Call login API
    */
    $.ajax({
        url: webapihost + "api/login",
        type: "POST",
        data: JSON.stringify({
            /*
             Following are the Credentials required to connect to the WebAPI server. Valiable model contains the credensial as they would be entered by an EBS user 
            */
            SubscriptionPassword: 'passx', 
            model: {
                BranchID: "BCN",
                LangID: "ES",
                UserID: "user",
                Password: "1"
            }
        }),
        contentType: "application/json; charset=utf-8",
        success: function (result) {

            token = result.Model.WebApiToken;
            document.getElementById("token").innerText = token;

            /*
            After successfull loging in you may process with thw other calls, you have a valid token and you may proceed with other calls.
            */
            
            _CreateOrder(token);
        },
        error: function (error) {
            document.getElementById("token").innerText = "error:" + JSON.stringify(error);
            console.log("error:" + JSON.stringify(error));
        }

    });

That code is a sample that is just one step of the process – getting the token. If you are manually dealing with tokens, then you will be saving it and using it in other api calls…Sorry I can’t help much with this sample.

No you alreday helped a lot. Truth is that their Docs are not very clear. Can’t you tell by this example how I might send the data through API server so I can connect?

Once again thank you very much!!

No, I can’t. For example:

var webapihost = "http://localhost/webapi2/";

This would typically be the url to use. However http://localhost won’t get you anywhere. :slight_smile:

Yes the URL is just a sample. They will have to provide a valid URL. This one is for local testing

I test API calls with Postman, to kind of test the tokens and stuff to make sure its talking correctly then I can setup my wappler stuff once I know my API syntax is correct.

https://www.getpostman.com/automated-testing

2 Likes

I had some experience connecting to my API for login page - details here - Unable to display Server side "API Action" data on my page. Might be helpful, please have a read.

Each API is different depending on it’s definition. But, it boils down to GET or POST calls with a combination of input parameters, query and headers.

yeah thats why I use the postman software to test my API calls first. Then once you know that you are forming them right, they snap into wappler pretty quick.

1 Like