# Authentication

Utilizing Your API Key&#x20;

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/>[.](https://api.vechainstats.com/v2/) You have two options for incorporating your API Key into REST API calls:

1. 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:

{% tabs %}
{% tab title="curl (bash)" %}

```bash
curl -X GET "https://api.vechainstats.com/v2/network/totals" -H "X-API-Key: YOUR_API_KEY_HERE"
```

{% endtab %}

{% tab title="java" %}

```java
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();
        }
    }
}

```

{% endtab %}

{% tab title="JavaScript (Node.js)" %}

```javascript
const https = require('https');

const apiKey = 'YOUR_API_KEY';
const apiUrl = 'https://api.vechainstats.com/v2/network/totals';

const options = {
  method: 'GET',
  headers: {
    'X-API-Key': apiKey,
  },
};

const req = https.request(apiUrl, options, (res) => {
  let data = '';

  res.on('data', (chunk) => {
    data += chunk;
  });

  res.on('end', () => {
    console.log(data);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
	"fmt"
	"net/http"
	"io/ioutil"
)

func main() {
	apiKey := "YOUR_API_KEY"
	apiURL := "https://api.vechainstats.com/v2/network/totals"

	req, err := http.NewRequest("GET", apiURL, nil)
	if err != nil {
		fmt.Println(err)
		return
	}

	req.Header.Set("X-API-Key", apiKey)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(string(body))
}

```

{% endtab %}

{% tab title="Python" %}

```python
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)
```

{% endtab %}

{% tab title="PHP" %}

```php
<?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);
?>
```

{% endtab %}
{% endtabs %}

Replace `"YOUR_API_KEY"` with your actual API key.

2. Convenience method: By including it as a query string parameter named VCS\_API\_KEY.&#x20;

```
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.

{% hint style="danger" %}
**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.
{% endhint %}
