Wednesday, 13 July 2022

Call external API in Firebase Cloud Function returns null

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?



from Call external API in Firebase Cloud Function returns null

No comments:

Post a Comment