
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
counterapi
Advanced tools
A lightweight, universal JavaScript client for CounterAPI - a simple API for tracking and managing counters.
npm install counterapi
import { Counter } from 'counterapi';
const { Counter } = require('counterapi');
<script src="https://cdn.jsdelivr.net/npm/counterapi/dist/counter.browser.min.js"></script>
<script>
// Counter is available as a global variable
const counter = new Counter({ workspace: 'my-workspace' });
</script>
// For v1 API
const counterV1 = new Counter({
version: 'v1', // Use v1 API
namespace: 'my-app', // Your namespace
debug: false, // Optional: Enable debug logging
timeout: 5000, // Optional: Request timeout in ms (default: 10000)
});
// For v2 API (default)
const counterV2 = new Counter({
workspace: 'my-workspace', // Your workspace name
debug: false, // Optional: Enable debug logging
timeout: 5000, // Optional: Request timeout in ms (default: 10000)
accessToken: 'your-token' // Optional: Authentication token for API requests
});
// Get the current value of a counter
const counter = await counterClient.get('page-views');
console.log(`Current count: ${counter.value}`);
// Increment a counter by 1
const counter = await counterClient.up('page-views');
console.log(`New count after increment: ${counter.value}`);
console.log(`Up count: ${counter.data.up_count}, Down count: ${counter.data.down_count}`);
// Decrement a counter by 1
const counter = await counterClient.down('page-views');
console.log(`New count after decrement: ${counter.value}`);
console.log(`Up count: ${counter.data.up_count}, Down count: ${counter.data.down_count}`);
// Set a counter to a specific value
const counter = await counterV1.set('page-views', 100);
console.log(`Counter set to: ${counter.value}`);
// Reset a counter to 0
const counter = await counterV2.reset('page-views');
console.log(`Counter reset to: ${counter.value}`);
// Get statistics for a counter
const result = await counterV2.stats('page-views');
console.log(`Up count: ${result.data.up_count}`);
console.log(`Down count: ${result.data.down_count}`);
console.log(`Today's up count: ${result.data.stats.today.up}`);
console.log(`This week's down count: ${result.data.stats.this_week.down}`);
console.log(`Most active hour: ${getMostActiveHour(result.data.stats.temporal.hours)}`);
Basic counter operations return a response with this structure:
{
code: "200",
data: {
created_at: "2025-06-17T11:33:23Z",
description: "",
down_count: 4,
id: 1,
name: "test",
slug: "test",
team_id: 4,
up_count: 4,
updated_at: "2025-06-17T11:33:23Z",
user_id: 7,
workspace_id: 1,
workspace_slug: "test"
}
}
The stats()
method returns a comprehensive statistics object:
{
code: "200",
data: {
id: 1,
counter_id: 1,
up_count: 6,
down_count: 4,
stats: {
today: {
up: 6,
down: 4
},
this_week: {
up: 6,
down: 4
},
temporal: {
hours: {
// Hourly breakdown (00-23)
"07": { up: 6, down: 4 },
// ... other hours
},
weekdays: {
// Day of week breakdown
"monday": { up: 0, down: 0 },
"tuesday": { up: 0, down: 0 },
"wednesday": { up: 6, down: 4 },
// ... other days
},
quarters: {
// Quarterly breakdown
"q1": { up: 0, down: 0 },
"q2": { up: 6, down: 4 },
"q3": { up: 0, down: 0 },
"q4": { up: 0, down: 0 }
}
}
},
created_at: "2025-06-17T11:33:23Z",
updated_at: "2025-06-18T07:44:11Z"
},
message: "Counter stats retrieved successfully"
}
Use standard Promise error handling:
try {
const counter = await counterClient.up('page-views');
} catch (error) {
console.error('Error:', error.message);
console.error('Status:', error.status);
console.error('Code:', error.code);
}
We provide ready-to-use examples to help you get started with CounterAPI:
The browser example demonstrates how to use CounterAPI in a web application:
To run the browser example:
# Build the library first
npm run build
# Serve the example using a web server
cd examples/browser
python -m http.server 8000
# Then open http://localhost:8000 in your browser
The Node.js example shows how to use CounterAPI in a server-side application:
To run the Node.js example:
# Build the library first
npm run build
# Run the example
cd examples/node
node index.js
You can use the stats data to create visualizations:
// Example: Creating a simple chart from hourly stats
function createHourlyChart(stats) {
const hours = stats.data.stats.temporal.hours;
const labels = Object.keys(hours).sort();
const upData = labels.map(hour => hours[hour].up);
const downData = labels.map(hour => hours[hour].down);
// Use your preferred charting library
renderChart({
labels,
datasets: [
{ label: 'Up', data: upData },
{ label: 'Down', data: downData }
]
});
}
// Example usage
const stats = await counter.stats('my-counter');
createHourlyChart(stats);
MIT
FAQs
JavaScript library for CounterAPI (node & browser)
We found that counterapi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.