Created
June 14, 2023 16:44
-
-
Save tofikhidayatxyz/e19163c80faca8680fe0a4e5bef6aa57 to your computer and use it in GitHub Desktop.
Get wise currency using google script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getWiseAPI(fromCurrency='EUR', toCurrency='IDR', price=1) { | |
| if(typeof fromCurrency != 'string') { | |
| fromCurrency = 'EUR' | |
| } | |
| if(typeof toCurrency != 'string') { | |
| toCurrency = 'EUR' | |
| } | |
| if(typeof price != 'number') { | |
| price=1 | |
| } | |
| try { | |
| const url = `https://api.wise.com/v1/rates?source=${fromCurrency}&target=${toCurrency}`; | |
| const headers = { | |
| "Authorization": "Basic OGNhN2FlMjUtOTNjNS00MmFlLThhYjQtMzlkZTFlOTQzZDEwOjliN2UzNmZkLWRjYjgtNDEwZS1hYzc3LTQ5NGRmYmEyZGJjZA==" | |
| }; | |
| const options = { | |
| method: "get", | |
| contentType: "application/json", | |
| muteHttpExceptions: true, | |
| headers: headers, | |
| }; | |
| const response = UrlFetchApp.fetch(url, options); | |
| const json = JSON.parse(response.getContentText()); | |
| const rate = parseFloat(json[0].rate) | |
| const total = parseFloat((price * rate).toFixed(3)) | |
| console.log(total) | |
| return total | |
| } catch (e) { | |
| Logger.log(e); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment