0

I am trying to simulate a REST api endpoint function where I want to check the endpoint for an exchange currency

Trying on postman

and i Do this https://api.flutterwave.com/v3/transfers/rates?amount=1000&destination_currency=USD&source_currency=KES

thereafter i input my secret key I get this as response

{
    "status": "success",
    "message": "Transfer amount fetched",
    "data": {
        "rate": 140.556,
        "source": {
            "currency": "KES",
            "amount": 140556
        },
        "destination": {
            "currency": "USD",
            "amount": 1000
        }
    }
}

Now i want to make this into a function, And i want to get the equivalient currency as shown above

See the function I am trying to simulate

const GetEqCurrency = async (source_currency, destination_currency, amount) => {
  var header = {
    Accept: "application/json",
    "Content-type": "application/json",
    Authorization:
      "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
  };

  var url = `https://api.flutterwave.com/v3/transfers/rates?amount=${amount}&destination_currency=${destination_currency}&source_currency=${source_currency}`;

  fetch(url, 
    { method: "GET", headers: header }).then((response) => response.json())
    .then((responseJson) =>{
      if(responseJson.message == 'Transfer amount fetched'){
        //console.log()
    
    // Help me output the equivalient data here in json
      }
    })
};

How do I output the following value in json Please help me here.

Pamela
  • 53
  • 5
  • This might help. https://stackoverflow.com/a/45366905/4025157 – MC Naveen Apr 24 '23 at 07:52
  • @MCNaveen looked at that, before i pasted this. Does not help in anyway – Pamela Apr 24 '23 at 07:52
  • I have no idea what is the problem – Konrad Apr 24 '23 at 07:53
  • @Pamela what is the response when you console log? – MC Naveen Apr 24 '23 at 07:55
  • @Konrad, i am creating a function. The function is supposed to simulate a GET request and then in Json, return the equivalient currency. For instance, I make a HTTP get Request to a server, then it is supposed to get the equivalient currency say from Kenyan Shillings to the USD and return the equivalient amount in Kenyan Shillings. That is what i want to achieve – Pamela Apr 24 '23 at 07:57
  • @MCNaveen, do not even see anything here – Pamela Apr 24 '23 at 07:58
  • You say "simulate" but you make a real request. What do you mean by "output"? Do you want to console.log it or return it from the function? And which part of the object do you want to output? – Konrad Apr 24 '23 at 08:05

2 Answers2

1

FYI - You can use Postman to generate sample snippets of your fetch request. Have a look here

Then try this:

const GetEqCurrency = async (source_currency, destination_currency, amount) => {
  var header = {
    Accept: "application/json",
    "Content-type": "application/json",
    Authorization:
      "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
  };

  var url = `https://api.flutterwave.com/v3/transfers/rates?amount=${amount}&destination_currency=${destination_currency}&source_currency=${source_currency}`;

  try {
    const response = await fetch(url, { method: "GET", headers: header });
    const responseJson = await response.json();
    if (responseJson.message == "Transfer amount fetched") {
      return responseJson.data;
    }
  } catch (error) {
    console.log(error);
  }
};


Herald Smit
  • 2,342
  • 1
  • 22
  • 28
  • Was somewhat what i needed somehow , but now, i am having some challenges using the same parameter to run the calculations. Will ask the question on a different thread – Pamela Apr 24 '23 at 10:46
0

You need to return what you need from the function:

const GetEqCurrency = async (source_currency, destination_currency, amount) => {
  const headers = {
    Accept: "application/json",
    "Content-type": "application/json",
    Authorization:
      "Bearer FLWSECK_TEST-153740d351951b5f6d5ae8b903e0c467-X",
  };

  // saves you from composing the query string manually
  const params = new URLSearchParams({
    amount,
    destination_currency,
    source_currency,
  });  

  const url = `https://api.flutterwave.com/v3/transfers/rates?${params}`;
  const response = await fetch(url, { headers });
  const parsed = await response.json();

  if (parsed.message !== 'Transfer amount fetched') {
    // or better throw an appropriate exception
    return null;
  }
  // return what you need from the response
  return parsed.data.destination.amount;
};
moonwave99
  • 21,957
  • 3
  • 43
  • 64