Querying Google BigQuery from Cloudflare Workers

We were trying to write a function which is triggered upon receiving an HTML request in Cloudflare workers, and queries BigQuery and returns the result. I was wondering if anyone has tried doing this before and how they got it working?

For reference, we’ve added this to index.js

if (url.pathname.startsWith('/query')) {
    return apiRouter.handle(request);
}

And we’ve added this to router.js

function query_bigquery() {

	const {BigQuery} = require('@google-cloud/bigquery');
	const bigquery = new BigQuery();

	// [END bigquery_client_default_credentials]
	async function query() {
  
		const query = `SELECT * FROM \`table name\`;
		const options = {
			query: query,
			location: 'US',
			projectId: '<project name>'
		};
	
		// Run the query as a job
		const [job] = await bigquery.createQueryJob(options);
		console.log(`Job ${job.id} started.`);
	
		// Wait for the query to finish
		const [rows] = await job.getQueryResults();
	
		// Print the results
		console.log(`Job ${job.id} finished.`)
		return rows
	}
	// [END bigquery_query]
	return query();
  }

// Jeep tracking queries
router.get('/query', () => new Response(JSON.stringify(query_bigquery())));

When I send fetch('https://website_name.workers.dev/query') in the Google Chrome console of our website, we get a 500 (Internal Server Error) back. Also, we tried just returning a string from within the function, so the problem appears to be from BigQuery itself. Would anyone have done this before?

Thank you!