Converting API request from curl to python
I have recently created an app in streamlit using python to fetch the latest exchange rates from Remitly and Wise and then carry out some comparisons to see who’s the best provider. The initial challenge I faced was that the API documentation provided by Remitly had the information using curl. I do not know curl so I had to translate this into python. I used the following website to help me get started: https://curlconverter.com/
So the API using curl is like the below
curl -H “X-API-KEY: <your-key-here>” “https://corridor-package-info.remitly.com/v0/packages/USA-PHL"
Using python requests we need to first define the header paylod as the API Key will be sent in the header as part of the request
import requests headers = { ‘X-API-KEY’: ‘<your-key-here>’, } response = requests.get(‘https://corridor-package-info.remitly.com/v0/packages/USA-PHL', headers=headers)
Now if you had other things to add into the header like your business ID, the content/application you’d put it as part of the headers dictionary and then your get request will use that. So this is an example turning a curl request to python using headers.
What if you had to do basic auth in your get request?
For instance, you were given a username and password like this
Username = YourName
Password = YourPassword
In such cases, you’d use the auth option in the get request and embed your username and password like below
import requestsurl = ‘https://api.transferwise.com/v3/comparisons/?sourceCurrency='GBP'&targetCurrency='INR'&sendAmount='100'response = requests.get(url ,auth=(‘YourName’, ‘YourPassword’))
So thats a quick overview on how to convert curl to python requests or use the basic auth option if necessary depending on where you are pulling the data and how their setup is