API
Documentation for the window.lantern.api object, used for making authenticated requests to the Lantern backend.
API (window.lantern.api
)
The window.lantern.api
object is your primary tool for communicating with the Lantern backend from the storefront. It provides a set of methods to perform actions and fetch data, with authentication handled automatically.
Available Methods
The api
object comes pre-configured with several methods tailored for common storefront operations.
Method | Description |
---|---|
getAccountActivity | Fetches the user's account activity feed. |
updateProfile | Updates the user's profile information. |
Usage Example
All API methods are asynchronous and return a Promise
. You should use async/await
or .then()
to handle the response.
Here's an example of how to fetch the current user's account activity.
async function displayAccountActivity() {
if (!window.lantern || !window.lantern.api) {
console.error('Lantern API is not available.');
return;
}
try {
const activity = await window.lantern.api.getAccountActivity();
console.log('Account Activity:', activity);
// Render the activity data in your theme...
} catch (error) {
console.error('Failed to fetch account activity:', error);
}
}
// Ensure Lantern is loaded before calling the function
window.addEventListener('lantern:loaded', displayAccountActivity);
Authentication
You do not need to handle authentication headers or tokens manually. The window.lantern.api
client automatically manages the user's session. When a user is logged into their Shopify account, the API client sends authenticated requests on their behalf. If the user is not logged in, the API will behave as an unauthenticated user.