This is a simple example of how to use CounterAPI in a browser environment.
- Increment and decrement a counter
- Get a counter's current value
- Reset a counter to zero
- Get counter statistics
- View the raw API responses
- Customizable workspace and counter name
- Build the CounterAPI library first:
# From the project root
npm run build- Serve the example using a web server:
# Using Python's built-in HTTP server
cd examples/browser
python -m http.server 8000
# Or using Node.js and http-server (npm install -g http-server)
cd examples/browser
http-server -p 8000- Open your browser and navigate to http://localhost:8000
The example demonstrates:
-
Loading the CounterAPI library in a browser environment
- Uses a local build during development
- Uses the CDN in production
-
Creating a Counter client instance with the default v2 API
- Uses the workspace parameter
-
Making API calls:
up()- Increments a counterdown()- Decrements a counterget()- Gets a counter's current valuereset()- Resets a counter to zerostats()- Gets counter statistics
-
Handling API responses and errors
- HTML/UI - Provides a simple interface with inputs and buttons
- JavaScript - Loads the library and handles user interactions
- CSS - Basic styling for a pleasant user experience
- The example uses async/await for API calls
- Error handling is implemented to show any API errors
- The workspace and counter name can be changed on the fly