Skip to main content

Overview

A common web application uses different validations that check the data format, uniqueness of records, validity of entered values, and so on. InCountry DRaaS provides resident functions that perform remote operations against regulated data and may return either the validation result without exposing actual regulated values or make some data transformations returning the transformed regulated values. In the latter case, you need to call a resident function from the application frontend, as sensitive values should not touch your application backend.

Let’s assume that you already have an existing function within your application at some endpoint that you call from the backend:

curl --request GET \
--url https://webapplication.com/api/customers/<email>/validate \
--header 'Authorization: Bearer <YOUR_SYSTEM_ACCESS_TOKEN>' \

This function performs a simple operation and validates whether the record with a specific email exists or not. In your system, email is regulated data, so it's saved to InCountry Vault. Now you need to check whether records with such an email already exist in InCountry Vault or not. You can do this as follows:

  1. Define a base URL to call a resident function that may look like the following

    const restApiUrl = country => `https://${country}-restapi-mt-01.api.incountry.io`;

    where country is a two-character code of the country (ISO format) (for example, us, ar, tr, and so on).

  2. Write a resident function using JavaScript:

    module.exports.handler = async (storage, country, params, modules) => {
    const result = await storage.find(country, { key3: params.email });
    if (result.records.length > 0) {
    return true;
    }
    return false;
    }
  3. Then publish it to InCountry DRaaS. You can do this through InCountry Portal.

    curl --location 'https://<restApiUrl>/serverless/publish' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer <INCOUNTRY_ACCESS_TOKEN>' \
    --data '{
    "scriptName": "GET /api/customers/<email>/validate",
    "scriptBody": "module.exports.handler=async(storage,country,params,modules)=>{const result = await storage.find(country,{key3:params.email });if(result.records.length>0){return true;}return false;}",
    "options": {
    "country": "<restApiCountryCode>",
    "forceUpdate": true
    }
    }'

    where restApiCountryCode is a two-character code of the country (ISO format) where you want to save the resident function. To call a resident function with the same HTTP request method and URL path as you do it earlier, you need to provide scriptName in the following format <method> <path_pattern>, for example:

    POST /customServerless/executeByCustomPath/<parameter_1>/<parameter_2>

    where <email> is a parameter.

    One more example of the script name:

    GET /api/customers/<email>/validate

    that can be executed via a request:

    curl --request POST \
    --url https://<restApiUrl>/run/customServerless/executeByCustomPath/<parameter_1>/<parameter_2> \
    --header 'Authorization: Bearer <INCOUNTRY_ACCESS_TOKEN>' \
  4. Execute the function when it is needed in your system by just replacing the original part of the GET request:

    curl --request GET \
    --url https://webapplication.com/api/customers/<email>/validate \
    --header 'Authorization: Bearer <YOUR_SYSTEM_ACCESS_TOKEN>' \

    where <email> equals to the customer’s email you want to validate, as follows:

    curl --request GET \
    --url 'https://<restApiUrl>/run/api/customers/<email>/validate' \
    --header 'Authorization: Bearer <INCOUNTRY_ACCESS_TOKEN>'
  5. Use the received result within your application’s business logic.

    {"result": "true"}

    or

    {"result": "false"}