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:
importjava.net.HttpURLConnection;importjava.net.URL;importjava.io.BufferedReader;importjava.io.InputStreamReader;publicclassAPIClient {publicstaticvoidmain(String[] args) {try {String apiKey ="YOUR_API_KEY";String apiUrl ="https://api.vechainstats.com/v2/network/totals";URL url =newURL(apiUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();// Set the request method to GETconnection.setRequestMethod("GET");// Set the API Key headerconnection.setRequestProperty("X-API-Key", apiKey);// Read the responseBufferedReader in =newBufferedReader(new InputStreamReader(connection.getInputStream()));String inputLine;StringBuilder response =newStringBuilder();while ((inputLine =in.readLine()) !=null) {response.append(inputLine); }// Close the connection and print the responsein.close();System.out.println(response.toString()); } catch (Exception e) {e.printStackTrace(); } }}
import requests# Set your API Key as a variableAPI_KEY ="YOUR_API_KEY"# Define the API endpointapi_url ="https://api.vechainstats.com/v2/network/totals"# Set the headers with the API Keyheaders ={"X-API-Key": API_KEY}# Make a GET request to the APIresponse = requests.get(api_url, headers=headers)# Print the responseprint(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 optionscurl_setopt($ch, CURLOPT_RETURNTRANSFER,true); // Return output as stringcurl_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 errorsif(curl_errno($ch)) {echo'cURL error: '.curl_error($ch);} else {// Decode JSON response $data =json_decode($response,true);// Print the responseprint_r($data);}// Close the cURL sessioncurl_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.