Docs Home | Open App
Cloudflare Visit Counter Setup
This project uses a first-party endpoint at /api/visits implemented in functions/api/visits.js.
Why this design
- No third-party random counter API dependency.
- Counter data is owned by your Cloudflare deployment.
- D1-backed persistence with cookie-based dedupe for practical uniqueness.
Required Cloudflare configuration
- Create a D1 database in Cloudflare (example:
image2cpp-visits).
- Open Pages project settings and add a D1 binding.
- Set binding name to
VISITS_DB.
- Deploy the project.
Endpoint behavior
GET /api/visits?op=hit&key=unique-visits-v1: increments when browser is not yet counted.
GET /api/visits?op=get&key=unique-visits-v1: reads value without increment.
On first counted hit, the response sets a cookie marker to reduce duplicate increments from the same browser profile.