skip to Main Content

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.

2

Answers


  1. 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;
    };
    
    Login or Signup to reply.
  2. 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);
      }
    };
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search