Skip to main content
Version: v2

Reconnecting with more BQL

Similar to connecting a 3rd-party library, you can also reconnect back and execute more BrowserQL as well. BQL does this by generating a new unique URL to use for running more queries. You can take this URL, append your API-token to it, and run more query language.

This is particularly useful for cases like AI where you might want to run several queries as users interact with your platform, but you don't want to necessarily keep an open connection.

Reconnecting with BQL

Below are examples of running two BQL queries on the same browser, in various languages:

const url = 'https://example.com';
const token = 'YOUR_API_TOKEN_HERE';
const timeout = 5 * 60 * 1000;

const queryParams = new URLSearchParams({
timeout,
token,
}).toString();
const endpoint = `https://production-sfo.browserless.io/chromium/bql`;
const variables = { url };
const query = `
mutation Reconnect($url: String!) {
goto(url: $url, waitUntil: networkIdle) {
status
}
reconnect(timeout: 30000) {
BypassQEEndpoint
}
}
`;
const queryTwo = `
mutation GetText {
text {
text
}
}
`;
const bql = async (url, query, variables) => {
const data = await fetch(`${url}?${queryParams}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
});

if (!data.ok) {
throw new Error(`Non OK response: ${await data.text()}`);
}

return data.json();
};

try {
console.log(`Running BQL Query#1: ${url}`);
const first = await bql(endpoint, query, variables);
const BypassQEEndpoint = first.data.reconnect.BypassQEEndpoint;

console.log(`Running BQL Query#2: ${BypassQEEndpoint}`);
const second = await bql(BypassQEEndpoint, queryTwo);
console.log(`Got results: "${second.data.text.text}"`);
} catch (error) {
console.error(error);
}