Overview
This tutorial walks through implementing Fingerprint to prevent coupon abuse, where customers or fraudsters repeatedly redeem a discount code meant for one-per-user use. You’ll begin with a starter app that includes a mock checkout page and a basic coupon flow. From there, you’ll add the Fingerprint JavaScript agent to identify each visitor and use server-side logic with Fingerprint data to check if they’ve already redeemed the coupon. This way, you can block repeat coupon redemptions from the same visitor. By the end, you’ll have a sample app that enforces one-time coupon usage per visitor and can be customized to fit your use case and business rules. This tutorial uses just plain JavaScript and a Node server with SQLite on the back end. For language- or framework-specific setups, see our quickstarts.Estimated time: < 15 minutes
Prerequisites
Before you begin, make sure you have the following:- A copy of the starter repository (clone with Git or download as a ZIP)
- Node.js (v20 or later) and npm installed
- Your favorite code editor
- Basic knowledge of JavaScript
1. Create a Fingerprint account and get your API keys
- Sign up for a free Fingerprint trial, or log in if you already have an account.
- After signing in, go to the API keys page in the dashboard.
- Save your public API key, which you’ll use to initialize the Fingerprint JavaScript agent.
- Create and securely store a secret API key for your server. Never expose it on the client side. You’ll use this key on the backend to retrieve full visitor information through the Fingerprint Server API.
2. Set up your project
- Clone or download the starter repository and open it in your editor.
Terminal
- This tutorial will be using the
coupon-abusefolder. The project is organized as follows:
Project structure
- Install dependencies:
Terminal
- Copy or rename
.env.exampleto.env, then add your Fingerprint API keys:
Terminal
- Start the server:
Terminal
- Visit http://localhost:3000 to view the mock checkout page from the starter app. You can test out the basic coupon flow by entering a valid coupon code (e.g.,
WELCOME20,SAVE10) and clicking Apply. You’ll notice the same coupon can currently be redeemed multiple times.
3. Add Fingerprint to the front end
In this step, you’ll load the Fingerprint client when the page loads and trigger identification when the user clicks Apply. The client returns both avisitorId and a requestId. Instead of relying on the visitorId from the browser, you’ll send the requestId to your server along with the coupon code. The server will then call the Fingerprint Events API to securely retrieve the full identification details, including the verified visitorId and risk signals such as browser tampering or bot activity.
- At the top of
public/index.js, load the Fingerprint JavaScript agent:
public/index.js
- Make sure to change
regionto match your workspace region (e.g.,eufor Europe,apfor Asia,usfor Global (default)). - Near the bottom of
public/index.js, the Apply button already has an event handler set up for submitting a coupon. Inside this handler, request visitor identification from Fingerprint using theget()method and include the returnedrequestIdwhen sending the coupon code to the server:
public/index.js
get() method sends signals collected from the browser to Fingerprint servers, where they are analyzed to identify the visitor. The returned requestId acts as a reference to this specific identification event, which your server can later use to fetch the full visitor details.
For lower latency in production, check out our documentation on using Sealed Client Results to return full identification details as an encrypted payload from the get() method.
4. Receive and use the request ID to get visitor insights
Next, pass therequestId through to your coupon validation logic, initialize the Fingerprint Server API client, and fetch the full visitor identification event so you can access the trusted visitorId and Smart Signals.
- In the back end, the
server/server.jsfile defines the API routes for the app. Update the/api/validate-couponroute there to also extractrequestIdfrom the request body and pass it into thevalidateCouponfunction.
server/server.js
- The
server/coupons.jsfile contains the logic for validating coupons. Start by importing and initializing the Fingerprint Server API client there, and load your environment variables withdotenv.
server/coupons.js
- Make sure to change
regionto match your workspace region (e.g.,EUfor Europe,APfor Asia,Globalfor Global (default)). - Update the
validateCouponfunction to acceptrequestIdand use it to fetch the full identification event details from Fingerprint:
server/coupons.js
requestId, the Fingerprint server client will retrieve the full data for the visitor identification request. The returned object will contain the visitor ID, IP address, device, and browser details, and Smart Signals like bot detection, browser tampering detection, VPN detection, and more.
You can see a full example of the event structure and test it with your own device in our demo playground.
For additional checks to ensure the validity of the data coming from your front end, view how to protect from client-side tampering and replay attacks in our documentation.
5. Block bots and suspicious devices
This optional step uses the Bot Detection and Suspect Score Smart Signals,
which are only available on paid plans.
event object includes the Bot Detection Smart Signal that flags automated activity, making it easy to reject bot traffic.
This signal returns good for known bots like search engines, bad for automation tools, headless browsers, or other signs of automation, and notDetected when no bot activity is found.
- Continuing in the
validateCouponfunction inserver/coupons.js, check the bot signal returned in theeventobject:
server/coupons.js
- Below the bot detection check, add a condition that reads the Suspect Score from the
eventobject and blocks the coupon if it exceeds a chosen threshold (for example, 20):
server/coupons.js
6. Prevent multiple uses of coupons per visitor
Next, use the trustedvisitorId from the event object to enforce one-time coupon usage per visitor. If the same visitorId tries to reuse the same coupon code, return a failure; otherwise, record the redemption and allow it. (This example simplifies coupon redemption logic for demonstration purposes.)
This makes coupon rules enforceable at the device level, not just the account level. Even if someone creates new accounts or checks out as a guest, the redemption is still linked to their device, keeping one-time coupons truly one-time.
Note: The starter app includes a SQLite database with these tables already created for you:
SQLite database tables
- Add some helper functions to the bottom of the
server/coupons.jsfile to check and record coupon redemptions:
server/coupons.js
- Update
validateCouponto retrieve thevisitorIdand use it to enforce the one-per-visitor coupon redemption rule and record successful redemptions:
server/coupons.js
This is a minimal example to show how to implement Fingerprint. In a real
application, make sure to implement proper security practices, error checking,
and data handling that align with your production standards.
7. Test your implementation
Now that everything is wired up, you can test the full protected coupon flow using the checkout page.- Start your server if it isn’t already running and open http://localhost:3000:
Terminal
- In the checkout form, enter a valid coupon (e.g.,
WELCOME20,SAVE10) and click Apply. You will see the discount applied. - Try applying the same coupon again. The second attempt will be rejected since the visitor has already redeemed that coupon.
- Open the page in incognito and try to use the coupon again, your visitor ID will remain the same and the redemption is still blocked.
- Bonus: Test the flow using a headless browser or automation tool to see bot detection in action. A sample script is available in
test-bot.js. While your app is running, run the script withnode test-bot.jsin your terminal and observe that the automated coupon redemptions are blocked.