Storage
The storage APIs can be used to store non-sensitive data that is persisted across command launches.
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.

API Reference

LocalStorage.getItem

Retrieve the stored value for the given key.

Signature

1
async function getItem(key: string): Promise<Value | undefined>;
Copied!

Example

1
import { LocalStorage } from "@raycast/api";
2
3
export default async () => {
4
const item = await LocalStorage.getItem<string>("favorite-fruit");
5
console.log(item);
6
};
Copied!

Parameters

Name
Type
Required
Description
key
string
Yes
The key you want to retrieve the value of.

Return

A Promise that resolves with the stored value for the given key. If the key does not exist, undefined is returned.

LocalStorage.setItem

Stores a value for the given key.

Signature

1
async function setItem(key: string, value: Value): Promise<void>;
Copied!

Example

1
import { LocalStorage } from "@raycast/api";
2
3
export default async () => {
4
await LocalStorage.setItem("favorite-fruit", "cherry");
5
};
Copied!

Parameters

Name
Type
Required
Description
key
string
Yes
The key you want to create or update the value of.
value
Value
Yes
The value you want to create or update for the given key.

Return

A Promise that resolves when the value is stored.

LocalStorage.removeItem

Removes the stored value for the given key.

Signature

1
async function removeItem(key: string): Promise<void>;
Copied!

Example

1
import { LocalStorage } from "@raycast/api";
2
3
export default async () => {
4
await LocalStorage.removeItem("favorite-fruit");
5
};
Copied!

Parameters

Name
Type
Required
Description
key
string
Yes
The key you want to remove the value of.

Return

A Promise that resolves when the value is removed.

LocalStorage.allItems

Retrieve all stored values in the local storage of an extension.

Signature

1
async function allItems(): Promise<Values>;
Copied!

Example

1
import { LocalStorage } from "@raycast/api";
2
3
interface Values {
4
todo: string;
5
priority: number;
6
}
7
8
export default async () => {
9
const items = await LocalStorage.allItems<Values>();
10
console.log(`Local storage item count: ${Object.entries(items).length}`);
11
};
Copied!

Return

A Promise that resolves with an object containing all Values.

LocalStorage.clear

Removes all stored values of an extension.

Signature

1
async function clear(): Promise<void>;
Copied!

Example

1
import { LocalStorage } from "@raycast/api";
2
3
export default async () => {
4
await LocalStorage.clear();
5
};
Copied!

Return

A Promise that resolves when all values are removed.

Types

LocalStorage.Values

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.

Properties

Name
Type
Description
[key: string]
any
The local storage value of a given key.

LocalStorage.Value

1
Value: string | number | boolean;
Copied!
Supported storage value types.

Example

1
import { LocalStorage } from "@raycast/api";
2
3
export default async () => {
4
// String
5
await LocalStorage.setItem("favorite-fruit", "cherry");
6
7
// Number
8
await LocalStorage.setItem("fruit-basket-count", 3);
9
10
// Boolean
11
await LocalStorage.setItem("fruit-eaten-today", true);
12
};
Copied!