Storage
All commands in an extension have shared access to the stored data. Extensions can not access the storage of other extensions.
Values can be managed through functions such as
LocalStorage.getItem
, LocalStorage.setItem
, or LocalStorage.removeItem
. A typical use case is storing user-related data, for example entered todos.The API is not meant to store large amounts of data. For this, use Node's built-in APIs to write files, e.g. to the extension's support directory.
Retrieve the stored value for the given key.
async function getItem(key: string): Promise<Value | undefined>;
import { LocalStorage } from "@raycast/api";
export default async function Command() {
await LocalStorage.setItem("favorite-fruit", "apple");
const item = await LocalStorage.getItem<string>("favorite-fruit");
console.log(item);
}
Name | Description | Type |
---|---|---|
key* | The key you want to retrieve the value of. | string |
A Promise that resolves with the stored value for the given key. If the key does not exist,
undefined
is returned.Stores a value for the given key.
async function setItem(key: string, value: Value): Promise<void>;
import { LocalStorage } from "@raycast/api";
export default async function Command() {
await LocalStorage.setItem("favorite-fruit", "apple");
const item = await LocalStorage.getItem<string>("favorite-fruit");
console.log(item);
}
Name | Description | Type |
---|---|---|
key* | The key you want to create or update the value of. | string |
value* | The value you want to create or update for the given key. |
A Promise that resolves when the value is stored.
Removes the stored value for the given key.
async function removeItem(key: string): Promise<void>;
import { LocalStorage } from "@raycast/api";
export default async function Command() {
await LocalStorage.setItem("favorite-fruit", "apple");
console.log(await LocalStorage.getItem<string>("favorite-fruit"));
await LocalStorage.removeItem("favorite-fruit");
console.log(await LocalStorage.getItem<string>("favorite-fruit"));
}
Name | Description | Type |
---|---|---|
key* | The key you want to remove the value of. | string |
A Promise that resolves when the value is removed.
Retrieve all stored values in the local storage of an extension.
async function allItems(): Promise<Values>;
import { LocalStorage } from "@raycast/api";
interface Values {
todo: string;
priority: number;
}
export default async function Command() {
const items = await LocalStorage.allItems<Values>();
console.log(`Local storage item count: ${Object.entries(items).length}`);
}
Removes all stored values of an extension.
async function clear(): Promise<void>;
import { LocalStorage } from "@raycast/api";
export default async function Command() {
await LocalStorage.clear();
}
A Promise that resolves when all values are removed.
Values of local storage items.
For type-safe values, you can define your own interface. Use the keys of the local storage items as the property names.
Name | Type | Description |
---|---|---|
[key: string] | any | The local storage value of a given key. |
Value: string | number | boolean;
Supported storage value types.
import { LocalStorage } from "@raycast/api";
export default async function Command() {
// String
await LocalStorage.setItem("favorite-fruit", "cherry");
// Number
await LocalStorage.setItem("fruit-basket-count", 3);
// Boolean
await LocalStorage.setItem("fruit-eaten-today", true);
}
Last modified 1mo ago