Store endpoints
This will explain how you will create a cart session that converts groceries to products and sends request to Foodin store.
Create a Cart Session
Add an endpoint on your server that creates a Cart Session. Cart Session runs through Product API and retrieves matching products. Regarding your Grocery data it will search through all subcategories in that exact store.
// This is your test secret API key.
const foodin = require('foodin')(‘YOUR_API_KEY');
const express = require('express');
const app = express();
app.use(express.static('public'));
Define groceries to search
Define product information when you create the Cart Session using predefined grocery IDs or on the fly with grocery.subcategory.
Create a shopping list
POST api.foodin.ai/store/{id}?items=${items}&location=${location}&userId=${userId}&name=${name}&email=${email}
Example Request
app.post("/shoppingList", (req, res) => {
const { items, location, userId, name, email } = req.body;
// Use the items, location, userId, name and email information to search for the store on the Foodin web
// Example using the request library to make a GET request to Foodin Product API
request.get(
"https://api.foodin.ai/store/{id}?items=${items}&location=${location}&userId=${userId}&name=${name}&email=${email}",
(err, response, body) => {
if (err) {
res
.status(500)
.json({ error: "Failed to search for store on Foodin." });
} else {
const store = JSON.parse(body);
// Redirect the client to the store on Foodin
res.redirect(store.url);
}
}
);
});
Create users
Sets up a user request to check if user exists on in Foodin User database
Create a user
POST api.foodin.ai/users
Example request
// Request to create a new user
app.post("/users", (req, res) => {
const { userId, name, email } = req.body;
// Validate that the request body contains all the required fields
if (!userId || !name || !email) {
res.status(400).json({ error: "Missing required fields." });
return;
}
// Verify that the userId, name, email are valid and not empty
if (userId.length === 0 || name.length === 0 || email.length === 0) {
res.status(400).json({ error: "Invalid input for userId, name or email." });
return;
}
// Verify that the email is in the correct format
if (!validateEmail(email)) {
res.status(400).json({ error: "Invalid email format." });
return;
}
// Check if the user already exists
User.findOne({ userId }, (err, existingUser) => {
if (err) {
res.status(500).json({ error: "Failed to check for existing user." });
return;
}
if (existingUser) {
res.status(400).json({ error: "User already exists." });
return;
}
// Create a new user if it doesn't exist
const newUser = new User({ userId, name, email });
newUser.save((err) => {
if (err) {
res.status(500).json({ error: "Failed to create new user." });
return;
}
res.json({ message: "User created successfully." });
});
});
});
The request body for creating a user includes the user's userId, name, and email, while the request query parameters are not required. The response of these request will be a JSON object that includes a success message or an error message depending on the outcome of the request. The endpoints are defined in a table format including the endpoint, method, request body, request query, response, and description.
Success 200
Field | Parameter | Description |
---|---|---|
userId | string | User unique ID |
name | string | User username |
string | User email |
Users address, location and other information aren’t needed to process the request, because the user leaves his information on Foodin