Code Snippets for HOPR Apps
Common Functions
Most HOPR applications require to do at least one of the following actions:
- Send commands to a HOPR node REST API via an Authenticated request
- Connect to a WebSocket HOPR and listen to responses sent there
- Prompt users for the HOPR node HTTP and WebSocket endpoints and API token
Since these actions are usually cumbersome, we have generated a few code snippets developers can copy and paste to execute most of these actions quickly. These snippets will eventually be included in an SDK1, but for the time being, having them here is the easiest and fastest way for anyone to get started.
useWebSocket.js
Loading useWebSocket.js
WebSocketHandler.jsx
Loading WebSocketHandler.jsx
getHeaders.js
Loading getHeaders.js
API Example Use
Here are some examples of how to use HOPR APIs with your terminal, Python or JS. HOPR is code agnostic so any language would work, you could even use API platforms like Postman to interact with your HOPR node.
accountWithdraw
- Terminal
- Python
- JavaScript
curl -X POST http://YOUR_API_URL:3001/api/v2/account/withdraw \
-H "Content-Type: application/json" \
-H "x-auth-token: YOUR_API_KEY" \
-d '{"amount": "1000000000000000000", "currency": "HOPR", "recipient": "YOUR_WALLET_ADDRESS"}'
import requests
import json
api_url = "https://YOUR_API_URL/api/v2/account/withdraw"
payload = json.dumps({
"amount": "1000000000000000000",
"currency": "HOPR",
"recipient": "YOUR_WALLET_ADDRESS"
})
headers = {
'x-auth-token': YOUR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.request("POST", api_url, headers=headers, data=payload)
const API_URL = 'http://YOUR_API_URL:3001/api/v2/account/withdraw';
const headers = {
'Content-Type': 'application/json',
'x-auth-token': 'YOUR_API_KEY',
};
const payload = {
amount: '1000000000000000000',
currency: 'HOPR',
recipient: 'YOUR_WALLET_ADDRESS',
};