To interact with the VeChainStats API, you have the flexibility to employ any server-side programming language capable of making HTTP requests. All requests must be directed towards the domain https://api.vechainstats.com/v2/. You have two options for incorporating your API Key into REST API calls:
Preferred approach: Through a dedicated custom header named X-API-Key.
In your application, you need to add the API Key to the header of your requests for authentication. Below we provide some code examples to add the API Key to the header in different environments:
curl -X GET "https://api.vechainstats.com/v2/network/totals" -H "X-API-Key: YOUR_API_KEY_HERE"
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class APIClient {
public static void main(String[] args) {
try {
String apiKey = "YOUR_API_KEY";
String apiUrl = "https://api.vechainstats.com/v2/network/totals";
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set the request method to GET
connection.setRequestMethod("GET");
// Set the API Key header
connection.setRequestProperty("X-API-Key", apiKey);
// Read the response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
// Close the connection and print the response
in.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import requests
# Set your API Key as a variable
API_KEY = "YOUR_API_KEY"
# Define the API endpoint
api_url = "https://api.vechainstats.com/v2/network/totals"
# Set the headers with the API Key
headers = {
"X-API-Key": API_KEY
}
# Make a GET request to the API
response = requests.get(api_url, headers=headers)
# Print the response
print(response.text)
<?php
$API_KEY = 'your_api_key_here';
$url = 'https://api.vechainstats.com/v2/network/totals';
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return output as string
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-API-Key: $API_KEY"
));
// Execute the cURL session and fetch the data
$response = curl_exec($ch);
// Check for cURL errors
if(curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
// Decode JSON response
$data = json_decode($response, true);
// Print the response
print_r($data);
}
// Close the cURL session
curl_close($ch);
?>
Replace "YOUR_API_KEY" with your actual API key.
Convenience method: By including it as a query string parameter named VCS_API_KEY.
Example request:
https://api.vechainstats.com/v2/account/vet-vtho
?address=0xd0d9cd5aa98efcaeee2e065ddb8538fa977bc8eb
&VCS_API_KEY=YOUR_API_KEY
Replace YOUR_API_KEY with your actual API key.
Security Alert: Safeguarding your API Key from public exposure is of utmost importance. For operational environments, it is strongly advised to utilize the custom header option instead of the query string method when transmitting your API Key.