2

I'm trying to call a simple api to currency conversion in Firebase Cloud Function in Typescript, but always it returns 'null'

import { https } from 'firebase-functions';
import * as axios from 'axios';
import * as cors from 'cors';

export const createTransfer = async (amount: number) => {

    https.onRequest((req, res) => {
        cors({origin: true})(req, res, () => {
            const config = {
                headers: {
                  apikey: 'APIKEY',
                },
                params: {
                    to: 'USD',
                    from: 'ILS',
                    amount: amount
                }
              };
            const convert = axios.default.get('https://api.apilayer.com/exchangerates_data/convert', config)
            .then((resp) => {
                res.send(resp.data);
            })
            .catch((error) => {
                res.sendStatus(error);
            });
            return convert;
        });
    });

};

/////// DEPLOYABLE FUNCTION ////////
export const stripeTransferPayment = https.onCall( async (data, context) => {
    const amount = assert(data, 'amount');

    return createTransfer(amount);
});

It should return the converted amount. Where am I doing wrong and how can I solve this?

Atoyansk
  • 233
  • 5
  • 20

2 Answers2

0

It looks like your line return convert; returns the value before it is ever assigned. Try awaiting on your request:

const convert = await axios.default.get('https://api.apilayer.com/exchangerates_data/convert', config); 
res.send(convert.data)
Faraji Anderson
  • 653
  • 6
  • 18
0

According to the docs at https://apilayer.com/marketplace/exchangerates_data-api I don't think you are providing the proper parameters in the request. First, it looks like you are attempting to convert the value in today's terms, but the endpoint you're using is for historical data. This probably isn't really a problem, and I'd expect you can continue using this endpoint if you also send it today's date. However, if you are trying to get a conversion for today, I would suggest using the https://api.apilayer.com/exchangerates_data/live endpoint instead.

According to the docs, the proper parameter names are "base" and "symbols", not "from" and "to" (respectively). Try this:

// ...
https.onRequest((req, res) => {
        cors({origin: true})(req, res, () => {
            const config = {
                headers: {
                  apikey: 'APIKEY', // ensure this is actually your API key in your code, good on you for not exposing it publicly ;)
                },
                params: {
                    base: 'USD',
                    symbols: 'ILS',
                    amount: amount
                }
              };
            const convert = axios.default.get('https://api.apilayer.com/exchangerates_data/live', config)
            .then((resp) => {
                res.send(resp.data);
            })
            .catch((error) => {
                res.sendStatus(error);
            });
            return convert;
        });
    });
    // ...

solidau
  • 4,021
  • 3
  • 24
  • 45
  • Unfortunately it keeps returning null... I can get the amount value, and the structure is following some examples I found – Atoyansk Jul 18 '22 at 13:09