Best practices
Pagination
It is important to be mindful of the number of requests being made to the server. Sending too many requests in a short period of time can put a strain on the server and lead to performance issues. To avoid this, it is recommended to implement a rate limiting mechanism in your code.
Parameters
Parameter | Description |
---|---|
limit | The number of groceries that should be returned in one page. |
search | The starting point of the groceries that should be returned in one page. For example, if the limit is set to 10 and the offset is set to 20, the function will return groceries 21-30. |
sort | The parameter that is used to sort the returned groceries. For example, you can sort by price or by name. |
preferences | A parameter that is used to filter the groceries by preferences. For example, if you want to return only vegan groceries, you can pass "gluten-free" as the dietary_restriction parameter. |
restrictions | A parameter that is used to filter the groceries by a specific dietary restriction. For example, if you want to return only gluten-free products, you can pass "gluten-free" as the dietary_restriction parameter. |
Request example
const https = require("https");
const baseUrl = "https://api.foodin.ai/groceries/";
// Set the pagination parameters
const limit = 10;
const page = 1;
const subcategory = "Rice";
const sort = "name";
const preferences = { is_organic: true };
const restrictions = ["gluten"];
// Add the pagination parameters to the query string
const url = `${baseUrl}
?limit=${limit}
&page=${page}
&subcategory=${subcategory}
&sort=${sort}
&preferences=${JSON.stringify(preferences)}
&restrictions=${JSON.stringify(restrictions)}`;
const req = https.request(url, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on("data", (d) => {
process.stdout.write(d);
});
});
req.on("error", (error) => {
console.error(error);
});
req.end();
Caching
Caching is built in our requests. You can access it from our Foodin Dashboard -> Account Settings. Click on enable caching and pick a location for caching files. Storing data locally in your device will improve the speed of listing groceries to your users. We update Groceries API once a year and you will receive a standard update notification to your account so you can clear out your cache every time it happens.
Use appropriate endpoints
When fetching a specific grocery, use the /groceries/{id}
endpoint, where {id}
is the unique identifier of the grocery item. This will directly retrieve the desired grocery item without the need to filter through a larger list of grocery items.
When searching for groceries based on specific parameters, such as name or category, use the /groceries/search endpoint with the appropriate query parameters. This will return a filtered list of grocery items that match the specified criteria.
When retrieving a list of all grocery items, use the /groceries/all endpoint with appropriate pagination and filtering parameters. This will return a limited number of items per page, making it easier to handle and display the large amount of data.
Additionally, it is important to be mindful of rate limits when making requests to the API. To avoid hitting rate limits and ensure consistent performance, consider implementing caching or implementing a request throttling mechanism.
It is also recommended to always check the API documentation and stay updated on any changes or updates to the endpoints and their usage to ensure optimal performance.
Handle Errors properly
Proper error handling is crucial for providing a seamless user experience when using the Foodin Grocery API. When making API requests, it is important to anticipate and handle any error responses that may be returned by the API. This can be done by implementing error handling logic in your code, such as displaying an appropriate message to the user or retrying the request after a certain amount of time. By handling errors in a user-friendly manner, you can ensure that the API is being used effectively and that your customers have a positive experience
User provided unit coversion
Use the function provided to you in Section 4.b convertUnits
. It is important to use the provided unit conversion feature in the Grocery API to ensure that the correct units are displayed to the user. This will ensure that the information presented is accurate and easily understandable. Additionally, it is important to choose the appropriate unit based on the context in which the information is being presented, such as using grams for weight measurements and cups for volume measurements. Proper use of unit conversion will provide a better user experience and increase the overall accuracy of the information presented.