Cookie management with API

  • Created

We recommend that you read the Getting started with Kameleo Automation article before reading this one.

Accessing cookies to export them to a .json file or importing cookies from another system to a Kameleo virtual browser profile can be useful. You won’t have to deal with different browser APIs you can simply use Kameleo Local API to do it.

You can export, import, or delete cookies of any virtual browser profile that is in terminated state. So it has been started at least once, but currently not running.

In this example we show how to list cookies, then modify a cookie and then delete all cookies.

Adding a cookie for a domain will override all the cookies for the given domain.

  • const cookieList = await client.cookie.listCookies(profile.id);
    console.log("The cookies of the profile: ", cookieList);
    
    const newCookie = cookieList[0];
    newCookie.value = "123";
    const cookiesArray = new Array(newCookie);
    await client.cookie.addCookies(profile.id, cookiesArray);
    
    await client.cookie.deleteCookies(profile.id);
  • var cookieList = await client.Cookie.ListCookiesAsync(profile.Id);
    Console.WriteLine("The cookies of the profile: ");
    foreach (var cookie in cookieList)
    {
        Console.WriteLine($"{cookie.Domain}, {cookie.Path}, {cookie.Name}");
    }
    
    var newCookie = cookieList[0];
    newCookie.Value = "123";
    var cookiesArray = new List { new CookieRequest(newCookie) };
    await client.Cookie.AddCookiesAsync(profile.Id, cookiesArray);
    
    await client.Cookie.DeleteCookiesAsync(profile.Id);
  • cookie_list = client.cookie.list_cookies(profile.id)
    print(f'There are {len(cookie_list)} cookies in the profile')
    
    cookie = cookie_list[0]
    new_cookie = CookieRequest(domain=cookie.domain, name=cookie.name, path=cookie.path, value=cookie.value,
                               host_only=cookie.host_only, http_only=cookie.http_only, secure=cookie.secure,
                               same_site=cookie.same_site, expiration_date=cookie.expiration_date)
    cookie_array = [new_cookie]
    client.cookie.add_cookies(profile.id, cookie_array)
    
    client.cookie.delete_cookies(profile.id)

Was this article helpful?

1 out of 2 found this helpful