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")
ParameterTypeRequiredDescription
KeyStringYesThe key name of the stored value to retrieve. Example: "gender".

Structure of Response

StatusResponse
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

  1. Check for null: Always verify that res.value exists before using it.
  2. Keep keys consistent: Use the same naming conventions as in setLocalStorage.
  3. Use for personalization: Great for restoring last-viewed product or theme preference.
  4. Combine with fallbacks: If value is missing, load defaults gracefully.
  5. Cache efficiently: Retrieve only what’s needed for optimal performance.

Caveats

  1. Missing key: Returns null if the specified key doesn’t exist.
  2. Device-local only: Data is stored per device — not synced between users or sessions.
  3. Not encrypted: Only use for non-sensitive data.
  4. Async behavior: Always handle Promises to avoid silent errors.
  5. Limited scope: Works exclusively inside the Superfans app environment.