The Counter API provides a integer counter service for your application. The counters are stored in our database and can be accessed via IDs unique to the provided API key. Counter that have not been read or updated in 180 days will be deleted permanently.
HTTP GET
Fetches and possibly update a counter.
id
(required) - id to specify the counter. Use a new id
to create a new counter.
hit
(optional) - whether to increase the count by 1. If used, must be set to true
.
value
(optional) - set the count to a specific integer value. Setting the value to 0 resets the counter.
X-Api-Key
(required) - API Key associated with your account.
https://api.api-ninjas.com/v1/counter?id=test_id&hit=true
{
"id": "test_id",
"value": 38
}
import requests
api_url = 'https://api.api-ninjas.com/v1/counter?id=test_id&hit=true'
response = requests.get(api_url, headers={'X-Api-Key': 'YOUR_API_KEY'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)
$.ajax({
method: 'GET',
url: 'https://api.api-ninjas.com/v1/counter?id=test_id&hit=true',
headers: { 'X-Api-Key': 'YOUR_API_KEY'},
contentType: 'application/json',
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});
const request = require('request');
request.get({
url: 'https://api.api-ninjas.com/v1/counter?id=test_id&hit=true',
headers: {
'X-Api-Key': 'YOUR_API_KEY'
},
}, function(error, response, body) {
if(error) return console.error('Request failed:', error);
else if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
else console.log(body)
});
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.fasterxml.jackson.databind.ObjectMapper;
URL url = new URL("https://api.api-ninjas.com/v1/counter?id=test_id&hit=true");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
InputStream responseStream = connection.getInputStream();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(responseStream);
System.out.println(root.path("fact").asText());
let url = URL(string: "https://api.api-ninjas.com/v1/counter?id=test_id&hit=true"!)!
var request = URLRequest(url: url)
request.setValue("YOUR_API_KEY", forHTTPHeaderField: "X-Api-Key")
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()