Here you go
Code:
# Replace 'YOUR_API_KEY' with your actual API key
api_key = 'YOUR_API_KEY'
# Set the endpoint URL
api_url = 'https://api.simpleswap.io/v1/'
# Function to get the current Bitcoin exchange rate
def get_btc_exchange_rate():
endpoint = 'get_rate'
currency_from = 'btc'
currency_to = 'usd'
# Construct the API request URL
url = f'{api_url}{endpoint}?currency_from={currency_from}¤cy_to={currency_to}'
# Make the API request using the constructed URL
response = requests.get(url, headers={'Api-Key': api_key})
# Process the API response
if response.status_code == 200:
data = response.json()
rate = data['rate']
print(f'Current BTC exchange rate: {rate}')
else:
print(f'Error: {response.status_code}')
# Function to swap Bitcoin for another cryptocurrency
def swap_bitcoin(address_from, address_to, amount):
endpoint = 'swap'
currency_from = 'btc'
currency_to = 'eth'
# Construct the API request URL
url = f'{api_url}{endpoint}'
# Set the request payload
payload = {
'address_from': address_from,
'address_to': address_to,
'amount': amount,
'currency_from': currency_from,
'currency_to': currency_to
}
# Make the API request using the constructed URL and payload
response = requests.post(url, headers={'Api-Key': api_key}, json=payload)
# Process the API response
if response.status_code == 200:
data = response.json()
transaction_id = data['transaction_id']
print(f'Success! Transaction ID: {transaction_id}')
else:
print(f'Error: {response.status_code}')
# Example usage
address_from = 'YOUR_BITCOIN_ADDRESS'
address_to = 'YOUR_ETHEREUM_ADDRESS'
amount = 0.01
get_btc_exchange_rate()
swap_bitcoin(address_from, address_to, amount)