Basics
Initialization

Initialization of bard-ai

Make sure you read through the Authentication page first, if you haven't already.

After obtaining your key, you are going to have to initialize it, so that bard-ai knows it. You key remains local on your device, so no one else can see it.

Example

import Bard from "bard-ai";
 
let myBard = new Bard(COOKIE);

Parameters

ParameterTypeDescription
keystringThe COOKIE you are initializing with. Required.
configobjectSome advanced options you can pass in.

Usage

Simply create a new instance of Bard, and pass in your COOKIE.

⚠️

It is recommended to store the key in a secure location (.env, for example), and then access it in your file indirectly. This will limit accidentally committing or sending your key to GitHub or other platforms.

Using a Different Cookie

bard-ai v2 comes with the ability to use cookies from different Google users. These will take the form of a __Secure-#PSID where X is a different number than 1. If you wish to use such a cookie, simply pass in an object instead of a string for your cookie, and set the key of the object with __Secure-#PSID= (where # is your account number), and the value being your COOKIE. Your account number is the number after the bard.google.com/u/.

For example, if you use Bard manually and the URL for Google Bard is bard.google.com/u/3, you would copy the cookie that starts with __Secure-3PSID.

Here's an example:

import Bard from "bard-ai";
 
let myBard = new Bard({
	"__Secure-3PSID": MY_COOKIE,
});

Note that since each cookie session is encapsulated within its own Bard instance, you can use multiple different cookies at the same time.

Using Additional Cookies

If bard-ai still doesn't work for you despite having checked that you are using the correct cookie, we recommend first resetting all cookies on bard.google.com, log in again, and then get your new __Secure-#PSID. However, this may still not work. In this case, you will need to use additional cookies. Here's how it works:

Step 1

Send any query on bard.google.com. This will generate a new __Secure-#PSIDTS cookie.

Step 2

Copy that cookie, and using the object method shown above, add it to your bard initialization:

import Bard from "bard-ai";
 
let myBard = new Bard({
	"__Secure-1PSID": MY_COOKIE_1PSID,
	"__Secure-1PSIDTS": MY_COOKIE_1PSIDTS,
});

That should make it work... ensure you do not refresh your bard.google.com page, because that will cause the TS cookie to re-generate, and your key will not work.

If these procedures still do not work, submit an issue to us!

Config

Pass in a configuration JSON, like this:

let myBard = new Bard(COOKIE, {
	verbose: true,
	fetch: fetch,
});
Config OptionTypeDescription
verbosebooleanLog information about what is happening
fetchfunctionAny fetch polyfill. Ensure it has the same interface as browser fetch.