react-native-nitro-cookies
July 5, 2026 ยท View on GitHub
High-performance HTTP cookie management for React Native using Nitro Modules JSI architecture.
Features
- 5x+ Faster than bridge-based cookie libraries thanks to JSI (JavaScript Interface)
- Synchronous API for performance-critical code paths (no async/await needed!)
- Cross-platform support for iOS (11+) and Android (API 21+)
- WebView synchronization with iOS WKWebView cookie storage
- Automatic HTTP header parsing from Set-Cookie headers
- Type-safe API with full TypeScript support
- Drop-in replacement for
@react-native-cookies/cookies
Installation
npm install react-native-nitro-cookies react-native-nitro-modules
# or
yarn add react-native-nitro-cookies react-native-nitro-modules
Note:
react-native-nitro-modulesis required as this library relies on Nitro Modules.
iOS
cd ios && pod install
Android
No additional setup required - autolinking handles everything.
Quick Start
import NitroCookies from "react-native-nitro-cookies";
// Set a cookie
await NitroCookies.set("https://example.com", {
name: "session_token",
value: "abc123",
path: "/",
secure: true,
});
// Get cookies for a URL
const cookies = await NitroCookies.get("https://example.com");
// Clear all cookies
await NitroCookies.clearAll();
API Overview
Synchronous Methods
For performance-critical code paths where you need immediate results without async overhead:
// Get cookies - returns Cookies dictionary (keyed by cookie name)
const cookies = NitroCookies.getSync("https://example.com");
// Set cookie - returns boolean immediately
NitroCookies.setSync("https://example.com", {
name: "session",
value: "abc123",
});
// Parse Set-Cookie header
NitroCookies.setFromResponseSync("https://example.com", "session=abc; path=/");
// Remove specific cookie
NitroCookies.clearByNameSync("https://example.com", "session");
// Set several cookies at once
NitroCookies.setManySync("https://example.com", [
{ name: "session", value: "abc123" },
{ name: "theme", value: "dark" },
]);
// Get the ready-to-send Cookie request header (e.g. "session=abc123; theme=dark")
const header = NitroCookies.getCookieHeaderSync("https://example.com");
Asynchronous Methods
For operations requiring WebKit access (iOS), network requests, or callback-based Android APIs:
| Method | Description |
|---|---|
get(url, useWebKit?) | Get cookies for URL |
set(url, cookie, useWebKit?) | Set a cookie |
setMany(url, cookies, useWebKit?) | Set several cookies for a URL |
getCookieHeader(url, useWebKit?) | Get the Cookie request-header string |
clearAll(useWebKit?) | Clear all cookies |
clearByName(url, name, useWebKit?) | Remove specific cookie |
setFromResponse(url, header) | Parse Set-Cookie header |
getFromResponse(url) | Fetch URL and extract cookies |
getAll(useWebKit?) | Get all cookies (iOS only) |
flush() | Persist cookies to disk (Android only) |
removeSessionCookies() | Remove session cookies (Android only) |
When to Use Sync vs Async
| Scenario | Recommended |
|---|---|
| Quick cookie read during render | getSync() |
| Setting cookie before navigation | setSync() |
| WebKit cookie store access (iOS) | get() / set() with useWebKit: true |
| Clearing all cookies | clearAll() |
| Fetching cookies from network | getFromResponse() |
Cookie Object
interface Cookie {
name: string; // Required
value: string; // Required
path?: string; // Defaults to "/"
domain?: string; // Defaults to URL host
secure?: boolean; // HTTPS only
httpOnly?: boolean; // No JS access
expires?: string; // ISO 8601 format
}
Attaching cookies to a manual request
getCookieHeader returns the exact value of the HTTP Cookie request header for
a URL, so you can forward stored cookies on a fetch or XMLHttpRequest you build
yourself:
const header = await NitroCookies.getCookieHeader("https://api.example.com");
await fetch("https://api.example.com/profile", {
headers: header ? { Cookie: header } : {},
});
On iOS the header is built with HTTPCookie.requestHeaderFields(with:), so name/value
serialization follows the platform's RFC 6265 rules; on Android it maps directly to
CookieManager.getCookie(url). The synchronous getCookieHeaderSync is available for
hot paths.
WebView Integration (iOS)
Manage cookies separately for native HTTP requests and WKWebView:
// For WKWebView - accessible in WebView
await NitroCookies.set(url, cookie, true); // useWebKit = true
// For native URLSession - not visible in WebView
await NitroCookies.set(url, cookie, false); // useWebKit = false
Error Handling
try {
await NitroCookies.set("example.com", cookie); // Missing protocol!
} catch (error) {
// INVALID_URL: URLs must include protocol (http:// or https://)
}
| Error Code | Description |
|---|---|
INVALID_URL | URL malformed or missing protocol |
DOMAIN_MISMATCH | Cookie domain doesn't match URL |
WEBKIT_UNAVAILABLE | WebKit requested on iOS < 11 |
PLATFORM_UNSUPPORTED | Platform-specific method on wrong platform |
NETWORK_ERROR | HTTP request failed |
Migration from @react-native-cookies/cookies
Drop-in replacement - just change the import:
- import CookieManager from '@react-native-cookies/cookies';
+ import CookieManager from 'react-native-nitro-cookies';
// All existing code works unchanged!
Prior Art
This package is a from-scratch Nitro Modules implementation, not a fork, but its public API and behavior are modeled on @react-native-cookies/cookies. Big thanks to its maintainers and contributors for the original implementation and long-term work on the project.
Acknowledgement
Example App
cd example && yarn install
# iOS
yarn ios
# Android
yarn android
TypeScript
import NitroCookies, {
type Cookie,
type Cookies,
type CookieError,
type CookieErrorCode,
} from "react-native-nitro-cookies";
License
MIT
Credits
Built with Nitro Modules by Marc Rousavy