Skip to Content
Cookies

Cookie Management

Cookie management is essential for making authenticated requests to the Genshin Impact API. This guide covers different approaches to handle cookies effectively in your application.

Overview

The genshin-kit.js library provides flexible cookie management options:

  • Simple single cookie: For basic usage with one set of credentials
  • Multiple cookies: To distribute requests and avoid rate limits
  • Custom cookie manager: For advanced cookie handling and browser integration

For simple applications, you can set a single cookie when creating the client:

const { Client } = require("genshin-kit.js"); // import { Client } from "genshin-kit.js"; // TypeScript const client = new Client(); client.login("LTUID", "LTOKEN");

Multiple Cookies

Why Use Multiple Cookies?

Using multiple cookies helps you:

  • Avoid rate limits: Distribute requests across different accounts
  • Increase reliability: Fallback to other cookies if one fails
  • Handle high-volume applications: Scale your requests effectively

Adding Multiple Cookies

const { Client } = require("genshin-kit.js"); // import { Client } from "genshin-kit.js"; // TypeScript const client = new Client(); // Add multiple cookies at once client.addCookies([ { ltoken: "LTOKEN_1", ltuid: "LTUID_1" }, { ltoken: "LTOKEN_2", ltuid: "LTUID_2" }, { ltoken: "LTOKEN_3", ltuid: "LTUID_3" } ]);

When you have multiple cookies, the client automatically:

  1. Uses cookies in sequential order (first, second, third, etc.)
  2. Cycles back to the first cookie after using all cookies

For advanced use cases, you can create a custom cookie manager with full control over cookie handling.

Basic Custom Manager

const { Client, ClientCookieManager } = require("genshin-kit.js"); // import { Client, ClientCookieManager } from "genshin-kit.js"; // TypeScript // Create a custom cookie manager const manager = new ClientCookieManager(); // Add cookies individually manager.setCookie("LTUID_1", "LTOKEN_1"); manager.setCookie("LTUID_2", "LTOKEN_2"); // Create client with custom manager const client = new Client({ cookieManager: manager });

The ClientCookieManager provides several useful methods:

const manager = new ClientCookieManager(); // Add a cookie manager.setCookie("LTUID", "LTOKEN"); // Get all stored cookies const allCookies = manager.getAll(); console.log("Total cookies:", allCookies.length); // Get the current cookie for requests const currentCookie = manager.get(); console.log("Current cookie:", currentCookie); // Remove a specific cookie by index manager.delete(0); // Removes the first cookie // Clear all cookies manager.clear(); // Check if manager has any cookies const hasCookies = manager.getAll().length > 0;

Getting Cookies from Chrome

You can automatically extract cookies from your Chrome browser, eliminating the need to manually copy them.

Prerequisites

  1. Install the required dependency:
npm install chrome-cookies-secure
  1. Make sure you’re logged into HoYoLab in Chrome
  2. Close Chrome completely before running the extraction

Implementation

const { Client, ClientCookieManager } = require("genshin-kit.js"); async function setupBrowserCookies() { const manager = new ClientCookieManager(); try { // Extract cookies from Chrome // Replace "Default" with your Chrome profile name if different await manager.getBrowserCookie("Default"); console.log("Successfully loaded cookies from Chrome"); console.log("Total cookies found:", manager.getAll().length); return new Client({ cookieManager: manager }); } catch (error) { console.error("Failed to load browser cookies:", error.message); throw error; } } // Usage async function main() { try { const client = await setupBrowserCookies(); // Now you use the client to make requests // ... } catch (error) { console.error("Error:", error.message); } } main();

Finding Your Chrome Profile

Chrome profiles are typically located at:

  • Windows: %LOCALAPPDATA%\Google\Chrome\User Data\
  • macOS: ~/Library/Application Support/Google/Chrome/
  • Linux: ~/.config/google-chrome/

Common profile names:

  • Default (most common)
  • Profile 1, Profile 2, etc.
  • Custom profile names you’ve created