Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google Cloud Datastore API key

KO5290
New Member

Hi!

I am using Node.js. I want to connect to the Google Cloud Database from the Node.js outside Google to get the data in the Datastore. I was able to get the API key of the Google cloud Datastore API. However, I don't understand how to put the API key into Javascript. Please tell me how to do it. 

0 1 579
1 REPLY 1

The recommended method for authenticating a Google Cloud client library (such as the Datastore client) depends on which environment you would like to deploy an application. In local development, for example, the recommended and safe way would be to use the Cloud CLI to generate Application Default Credentials. The client automatically uses authentication credentials generated by this method. You can then use the following code from this sample, and it would be authenticated.

const {Datastore} = require('@google-cloud/datastore');

// Creates a client (using application default credentials)
const datastore = new Datastore();

async function quickstart() {
  const kind = 'Task';
  const name = 'sampletask1';

  // The Cloud Datastore key for the new entity
  const taskKey = datastore.key([kind, name]);

  // Prepares the new entity
  const task = {
    key: taskKey,
    data: {
      description: 'Buy milk',
    },
  };

  await datastore.save(task);
  console.log(`Saved ${task.key.name}: ${task.data.description}`);
}
quickstart();

For different environments, this guide shows you recommended approaches for multiple environments.