Visitor Counter API
How to add the MilkAPI visitor counter to your own website.
1. Get Your API Key
First, register with our service to receive an API key. Then log on to your dashboard at https://api.cptmilk.xyz and generate an API key.
2. Display the Visitor Count
Returns the current count without changing it. Add the snippet below where you want the count to appear, and replace YOUR_API_KEY with your key.
<!-- 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 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>
3. Increment the Visitor Count
Adds one to the count and returns the new total. Call it once per page load, for example:
<script>
// This sends 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>
4. Domain Restrictions
If an API key has an allowed domain, requests must come from a page on that domain. The API checks the browser's Origin or Referer header, so make sure your website's domain matches the one set on your key.
5. Rate Limiting
Each visitor IP can increment a key at most 5 times every 5 minutes. Further POST requests in that window get a 429 "Rate limit exceeded" response. Reading the count with GET is not limited.
6. Responses
| Status | Meaning |
|---|---|
| 200 | {"visitor_count": 1234} |
| 400 | No API key was given. |
| 401 | The API key doesn't exist. |
| 403 | The request didn't come from the key's allowed domain. |
| 429 | Rate limit exceeded. Try again later. |
If you have any questions or need further help, please contact support.