Modern Cookies, Minus the Mess: A Quick Guide to the Cookie Store API

Collection of half chocolate chip cookies on white background
The Cookie Store API modernizes web cookies with async methods, typed objects, and change events—simplifying auth, prefs, and multi-tab sync.

If you’ve ever wrangled document.cookie, you know it’s a synchronous string mess that’s easy to misuse and hard to reason about. The Cookie Store API modernizes cookies with a promise-based, structured interface that works in window and service worker contexts. It enables async reads/writes, typed cookie objects, and change events—perfect for performance-sensitive and offline-capable apps.

What it looks like

Set a cookie asynchronously

				
					cookieStore.set({ name, value, expires, path, sameSite });
				
			

Returns a promise; no string parsing needed.

 Read a cookie

				
					cookieStore.get(name);
				
			

Resolves to a cookie object or null.

 List cookies

				
					cookieStore.getAll();
				
			

Returns an array of cookie objects.

Delete a cookie

				
					cookieStore.delete(name);
				
			

Removes it by key.

Listen for changes

				
					cookieStore.addEventListener('change', handler);
				
			

Fires when cookies are changed or deleted—even across tabs (where supported).

Walking through the demo

				
					// 1. Set a cookie asynchronously
const setCookie = async () => {
	await cookieStore.set({
		name: 'demoCookie',
		value: 'cookieStoreRocks',
		expires: Date.now() + 60 * 60 * 1000, // 1 hour from now
		path: '/',
		sameSite: 'lax'
	});
	console.log('Cookie set!');
};

// 2. Get a cookie by name
const getCookie = async () => {
	const cookie = await cookieStore.get('demoCookie');
	if (cookie) {
		console.log('Cookie value:', cookie.value);
	} else {
		console.log('Cookie not found.');
	}
};

// 3. List all cookies
const listCookies = async () => {
	const cookies = await cookieStore.getAll();
	console.log('All cookies:', cookies);
};

// 4. Delete a cookie
const deleteCookie = async () => {
	await cookieStore.delete('demoCookie');
	console.log('Cookie deleted!');
};

// 5. Listen for cookie changes (works in supported browsers)
cookieStore.addEventListener('change', (event) => {
	for (const changed of event.changed) {
		console.log('Cookie changed:', changed);
	}
	for (const deleted of event.deleted) {
		console.log('Cookie deleted:', deleted);
	}
});

// --- Demo sequence ---
(async () => {
	await setCookie();
	await getCookie();
	await listCookies();
	await deleteCookie();
	await listCookies();
})();

				
			

Why it’s better

Practical tips and caveats

Getting started snippet

Example feature detection

				
					if (!('cookieStore' in self)) {
    console.warn('Cookie Store API not supported; falling back to document.cookie.');
    // Provide polyfill or minimal fallback here.
}
				
			

Bottom line

The Cookie Store API brings cookies into the modern web platform: async, typed, and event-driven. Use it to simplify auth state, user preferences, and multi-tab coordination—just remember to feature-detect and keep security attributes tight.

More To Explore

Code

The Contact Picker API: Fast, private access to a user’s address book

The Contact Picker API lets mobile web apps open the native contacts chooser so users can share exactly the fields you request—like name, phone, email, address, or avatar—without granting blanket access to their entire address book. It’s user-initiated, privacy-preserving, and perfect for speeding up invites and form fills.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.