Get Local Storage
Gets a variable in local storage of the device
Description
The getLocalStorage action retrieves a stored value from the app’s local storage.
It allows you to access previously saved data — such as user preferences, app state, or temporary variables — for personalized and consistent experiences.
Function Signature
VajroSDK.actions.getLocalStorage("name")| Parameter | Type | Required | Description |
|---|---|---|---|
| Key | String | Yes | The key name of the stored value to retrieve. Example: "gender". |
Structure of Response
| Status | Response |
|---|---|
| Success | { "status": "success", "message": "Successfully retrieved the value from local storage", "value": "retrieved-value" } |
| Invalid Params | { "status": "error", "errorId": 400, "errorHandle": "invalid-params", "message": "Invalid params" } |
| Unexpected Error | { "status": "error", "errorId": 500, "errorHandle": "unknown-error", "message": "Something unexpected happened" } |
Example Usage
let discount_code="";
function getDiscountCode() {
return VajroSDK.actions.getLocalStorage("discount_code")
.then((val) => val || "")
.catch((error) => "");
}
function printLocalStorage(){
getDiscountCode().then((discount_code) => {
console.log("discount_code",discount_code)
document.getElementById("disc").innerText=discount_code;
VajroSDK.actions.showAlert({
title: "Discount Applied",
message: `Shopping for: ${discount_code}`
});
VajroSDK.actions.applyDiscount([discount_code]);
});
}
printLocalStorage();<div id="disc">the discount code is </div>Best Practices
- Check for null: Always verify that res.value exists before using it.
- Keep keys consistent: Use the same naming conventions as in setLocalStorage.
- Use for personalization: Great for restoring last-viewed product or theme preference.
- Combine with fallbacks: If value is missing, load defaults gracefully.
- Cache efficiently: Retrieve only what’s needed for optimal performance.
Caveats
- Missing key: Returns null if the specified key doesn’t exist.
- Device-local only: Data is stored per device — not synced between users or sessions.
- Not encrypted: Only use for non-sensitive data.
- Async behavior: Always handle Promises to avoid silent errors.
- Limited scope: Works exclusively inside the Superfans app environment.
Updated about 1 month ago