Skip to content

Fixer Example

The code below is an example of a process connecting to Fixer (via apilayer) to pull historical foreign exchange rates. Fixer authenticates with a simple API key header rather than MODLR's built-in OAuth 2.0 flow, so this is an Advanced Integration.

INFO

This process requires a Fixer API key, stored as a Secret rather than hardcoded in the process.

Process Code

js
function pre() {
    // This function is called once before the process is executed.
    // Use this to setup prompts.
    script.log('process pre-execution parameters parsed.');
    script.prompt("Base Currency", "baseCurrency", "AUD");
    script.prompt("Symbols (comma-separated)", "symbols", "USD,GBP,NZD");
    script.prompt("Start Date", "startDate", "2026-01-01");
    script.prompt("End Date", "endDate", "2026-01-31");
}

function begin() {
    // This function is called once at the start of the process
    script.log('process execution started.');

    const url = `https://api.apilayer.com/fixer/timeseries?start_date=${startDate}&end_date=${endDate}&base=${baseCurrency}&symbols=${symbols}`;

    const response = web.get(url, {
        "apiKey": security.getSecret("FIXER_API_KEY")
    });

    if (response.status >= 200 && response.status < 300) {
        const body = JSON.parse(response.body);

        if (body.success === false) {
            console.log("Fixer API error:", body.error);
            return;
        }

        console.log(body.rates);
    } else if (response.error) {
        // An error occurred while sending the request (e.g., network error, timeout, etc.).
        console.log("Error occurred while sending the request: " + response.error);
    } else {
        // The request failed with a non-success status code.
        console.log("Request failed with status code: " + response.status);
        console.log(response.body);
    }
}

function data(record) {
    // This function is called once for each line of data on the second cycle
    // Use this to build dimensions and push data into cubes
}

function end() {
    // This function is called once at the end of the process
    script.log('process execution finished.');
}