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',
};
accountBalances
- Terminal
- Python
- JavaScript
curl -X GET http://YOUR_API_URL:3001/api/v2/account/balances \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/account/balances"
headers = {'x-auth-token': 'YOUR_API_KEY'}
response = requests.request("GET", api_url, headers=headers)
const axios = require("axios");
const API_URL = 'http://YOUR_API_URL:3001/api/v2/account/balances';
const headers = {
'x-auth-token': 'YOUR_API_KEY',
};
axios.get(API_URL, { headers })
.then(response => console.log(response.data))
.catch(error => console.log(error));
accountAddresses
- Terminal
- Python
- JavaScript
curl -X GET http://YOUR_API_URL:3001/api/v2/account/addresses \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/account/addresses"
headers = {'x-auth-token': 'YOUR_API_KEY'}
response = requests.request("GET", api_url, headers=headers)
const axios = require("axios");
const API_URL = 'http://YOUR_API_URL:3001/api/v2/account/addresses';
const headers = {
'x-auth-token': 'YOUR_API_KEY',
};
axios.get(API_URL, { headers })
.then(response => console.log(response.data))
.catch(error => console.log(error));
Get Aliases
- Terminal
- Python
- JavaScript
curl -X GET http://YOUR_API_URL:3001/api/v2/aliases \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/aliases"
headers = {'x-auth-token': 'YOUR_API_KEY'}
response = requests.request("GET", api_url, headers=headers)
const axios = require("axios");
const API_URL = 'http://YOUR_API_URL:3001/api/v2/aliases';
const headers = {
'x-auth-token': 'YOUR_API_KEY',
};
axios.get(API_URL, { headers })
.then(response => console.log(response.data))
.catch(error => console.log(error));
Set Alias
- Terminal
- Python
- JavaScript
curl -X POST http://YOUR_API_URL:3001/api/v2/aliases \
-H "x-auth-token: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"peerId": "16Uiu2HAmUsJwbECMroQUC29LQZZWsYpYZx1oaM1H9DBoZHLkYn12", "alias": "Alice"}'
import requests
api_url = "https://YOUR_API_URL/api/v2/aliases"
headers = {'x-auth-token': 'YOUR_API_KEY', 'Content-Type': 'application/json'}
data = {'peerId': '16Uiu2HAmUsJwbECMroQUC29LQZZWsYpYZx1oaM1H9DBoZHLkYn12', 'alias': 'Alice'}
response = requests.request("POST", api_url, headers=headers, json=data)
const axios = require("axios");
const API_URL = 'http://YOUR_API_URL:3001/api/v2/aliases';
const headers = {
'x-auth-token': 'YOUR_API_KEY',
'Content-Type': 'application/json',
};
const data = {
peerId: '16Uiu2HAmUsJwbECMroQUC29LQZZWsYpYZx1oaM1H9DBoZHLkYn12',
alias: 'Alice',
};
axios.post(API_URL, data, { headers })
.then(response => console.log(response.data))
.catch(error => console.log(error));
channelsRedeemTickets
- Terminal
- Python
- JavaScript
curl -X POST http://YOUR_API_URL:3001/api/v2/channels/tickets/redeem \
-H "Content-Type: application/json" \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/channels/tickets/redeem"
headers = {
'x-auth-token': YOUR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.request("POST", api_url, headers=headers)
const API_URL = 'http://YOUR_API_URL:3001/api/v2/channels/tickets/redeem';
const headers = {
'Content-Type': 'application/json',
'x-auth-token': 'YOUR_API_KEY',
};
const response = await fetch(API_URL, {
method: 'POST',
headers: headers
});
channelsGetTickets
- Terminal
- Python
- JavaScript
curl -X GET http://YOUR_API_URL:3001/api/v2/channels/tickets \
-H "Content-Type: application/json" \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/channels/tickets"
headers = {
'x-auth-token': YOUR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.request("GET", api_url, headers=headers)
const API_URL = 'http://YOUR_API_URL:3001/api/v2/channels/tickets';
const headers = {
'Content-Type': 'application/json',
'x-auth-token': 'YOUR_API_KEY',
};
const response = await fetch(API_URL, {
method: 'GET',
headers: headers
});
channelsCloseChannel
- Terminal
- Python
- JavaScript
curl -X DELETE http://YOUR_API_URL:3001/api/v2/channels \
-H "Content-Type: application/json" \
-H "x-auth-token: YOUR_API_KEY"
import requests
api_url = "https://YOUR_API_URL/api/v2/channels"
headers = {
'x-auth-token': YOUR_API_KEY,
'Content-Type': 'application/json'
}
response = requests.request("DELETE", api_url, headers=headers)
const API_URL = 'http://YOUR_API_URL:3001/api/v2/channels';
const headers = {
'Content-Type': 'application/json',
'x-auth-token': 'YOUR_API_KEY',
};
const response = await fetch(API_URL, {
method: 'DELETE',
headers: headers
});