Get White Label API
Before you can use the API, you need two essential "ingredients":
Authorization
header of your API calls.
To begin using the chatbot on your product page, you'll first need to load the necessary JavaScript file. This script handles the user interface and logic for the bargaining functionality.
https://custom.dbargainapi.com/assets/js/script.js
Once the script is loaded, you need to pass information about the product and user to the chatbot. This is done by setting up the options object, which includes product details, user information, and the "Add to Cart" functionality.
function addToCartHandle() {
// Your existing Add to Cart function logic goes here
}
2. Next, create an options object that contains essential product and user information:
const options = {
product_id: "YOUR_PRODUCT_ID", // Unique ID of the product being viewed
variant_id: "YOUR_VARIANT_ID", // Variant ID (e.g., size or color)
product_image: "YOUR_PRODUCT_IMAGE_URL", // Image URL of the product
product_name: "YOUR_PRODUCT_NAME", // Name of the product
product_link: "YOUR_PRODUCT_URL", // Link to the product page
userid: "YOUR_USER_ID", // User ID (can be a logged-in user or session ID)
token: "YOUR_API_TOKEN", // Your API token for authenticating requests
currency_symbol: "Rs.", // The symbol for the currency (customizable)
product_price: "PRODUCT_PRICE", // Current price of the product
cart_link: "YOUR_CART_URL", // Link to your shopping cart page
cart: "YOUR_CART_ARRAY (OPTIONAL)", // Optional: Current cart items
addToCartHandle: addToCartHandle, // Function to handle adding items to cart
};
3. Initialize the chatbot by calling initDbargain(options).
initDbargain(options);
After a user interacts with the chatbot and adds items to their cart, you can fetch the available bargains and apply discounts directly to the cart. The chatbot uses a cookie named dbargainuser
to identify the user and their previous negotiations.
async function getBargainDiscounts(cart) {
const dbargainuser = getCookie('dbargainuser'); // Fetch 'dbargainuser' cookie
if (!dbargainuser) {
console.log('dbargainuser cookie not found');
return;
}
const postFields = {
dbargainuserid: dbargainuser, // User ID from cookie
shopid: 'YOUR_USER_ID', // Replace with your shop ID
cart: cart // Cart object containing product details
};
try {
const response = await fetch('https://custom.dbargainapi.com/api/getBargainedList', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_TOKEN', // Replace with your API token
'Content-Type': 'application/json'
},
body: JSON.stringify(postFields)
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching bargain discounts:', error);
}
}
2. You can then call this function when the cart is loaded on the cart page, or any time you need to update the bargain prices:
const dbargainuser = getCookie('dbargainuser');
if (dbargainuser) {
getBargainDiscounts(yourCartObject).then(discounts => {
console.log('Bargain discounts:', discounts);
});
}
To ensure you pass the correct information to the API, the cart object needs to contain key details about each product, such as its ID and variant. You can modify your existing cart object like this:
const cartArrayForBargain = cart.map(c => ({
product_id: 'YOUR_PRODUCT_ID', // Replace with actual product ID
variation_id: 'YOUR_PRODUCT_VARIANT_ID' // Replace with actual variant ID
}));
2. The cartArrayForBargain
array can then be passed to the getBargainDiscounts
function.
product_id
and a variation_id
, which are required for the bargaining process.
The bargaining API relies on cookies to track users and their negotiation history. You’ll need a utility function to retrieve the value of specific cookies, such as the dbargainuser
cookie.
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
return null;
}
dbargainuser
cookie is passed to the API for discount retrieval.
POST /api/getBargainedList
dbargainuserid
: The ID of the user making the request.shopid
: The ID of your shop.cart
: The cart object containing product and variant IDs.Authorization: Bearer YOUR_API_TOKEN
Content-Type: application/json
POST /api/destroy_bargained_list
dbargainuserid
: The user ID.shopid
: Your shop ID.cart
: Cart details.Authorization: Bearer YOUR_API_TOKEN
Content-Type: application/json
Got an Online Store?
Sign up to our newsletter
Receive the latest news, exclusive discounts & offers straight to your inbox!
© All Right Reserved | Made with ❤ by Website99