This page explains how you can add the visitor counter API to your own website. Follow the instructions below:
First, register with our service to receive an API key. Log in to your dashboard at https://api.cptmilk.xyz and generate an API key.
To display the current visitor count on your website, you can use the following JavaScript snippet. Make sure to replace YOUR_API_KEY with the API key you obtained.
<!-- Include this element where you want the visitor count to appear -->
<div id="visitor-counter">Loading visitor count...</div>
<script>
// Replace with your actual API endpoint and API key
const apiKey = 'YOUR_API_KEY';
const apiUrl = 'https://api.cptmilk.xyz/visit?api_key=' + apiKey;
// Fetch the current visitor count (does not increment)
fetch(apiUrl)
.then(response => response.json())
.then(data => {
document.getElementById('visitor-counter').textContent =
'Visitor Count: ' + data.visitor_count;
})
.catch(error => {
console.error('Error fetching visitor count:', error);
document.getElementById('visitor-counter').textContent = 'Error loading visitor count.';
});
</script>
If you want to increment the visitor count (for example, on page load), use a POST request. Here's an example using JavaScript's fetch:
<script>
// This code will send a POST request to increment the visitor count.
fetch('https://api.cptmilk.xyz/visit?api_key=' + apiKey, {
method: 'POST'
})
.then(response => response.json())
.then(data => {
console.log('Updated visitor count:', data.visitor_count);
// Optionally update the counter on your page here
})
.catch(error => console.error('Error incrementing count:', error));
</script>
Note that if the API key is configured with a domain restriction, the request must originate from that domain. Ensure your website's domain matches the one specified in your API key settings.
The API limits increments to one per IP per key every 5 minutes. If you call the POST endpoint too frequently, you may receive a "Rate limit exceeded" response.
If you have any questions or need further assistance, please contact support.
Happy counting!