How Can We Help?

Search for answers or browse about Sintel Forms.

Accessing external APIs

You are here:

It is possible to access any external API using the Logic feature in Sintel Forms. This can be used for retrieving some external information to be used in a form or to send some data to external services. External API calls can be made by writing custom JavaScript code in the Logic section of a form, using the “Execute custom JS” step:

 

executeCustomJsRule.png

 

Example calls

api.get(apiPath)
   .then(response => { /*handle success*/  })
   .catch( ex => { /* handle failure */ })
api.post(apiPath, data)
   .then(response => { /*handle success*/  })
   .catch( ex => { /* handle failure */ })

 

The “api” object is a wrapper of the “axios” object, meaning that you can use all the axios features described on its webpage.

Example – retrieving user data from external API

You can download the configuration of the form used in this example here, and import it in Sintel Forms on your tenant.

 

This example is about utilizing the external api calls to retrieve information about a random user from a test API and display it in the form.

We will be using the https://randomuser.me/ API, returning random user information.

 

  1. Set up the form
    We want the form to be displaying name, surname and picture of a random user. Therefore we make sure that a list contains the “FirstName”, “LastName” and “Photo” columns (first two being text fields and the last one being a url/picture field): 

     

  2. Prepare the layout
    Then we make sure that the fields are present on the form and that “FirstName” and “LastName” have more user-friendly titles: 

    layoutView.png

     

    Furthermore, to ensure that the photo field consistently displays an image rather than its URL, we explicitly set its mode to ‘read only’:

     

    fieldProperties.png

     

  3. Set up logic
    In a new Rule, we create an “Execute custom js function” step with following code: 

    let apiAddress = "https://randomuser.me/api/";
    api.get(apiAddress)
       .then(response => {
          const randomUser = response.data.results[0];
          setValue('FirstName', randomUser.name.first);
          setValue('LastName', randomUser.name.last);
          setValue('Photo', {url: randomUser.picture.large, description: ""});
        });

    Now, let’s ensure that this Rule doesn’t keep executing with every field change. To achieve this, we’ll add a restriction: it will only execute if the “FirstName” field remains unset. We’ll accomplish this by creating a “Field value check” condition within the Rule:

     

     

    That’s it – when we open a form, it will initialize itself with random values taken from the example API:

    exampleData.png

Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Table of Contents