Cloud Node.js Client
Node.js idiomatic client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services at a General Availability (GA) quality level:
This client supports the following Google Cloud Platform services at a Beta quality level:
This client supports the following Google Cloud Platform services at an Alpha quality level:
If you need support for other Google APIs, check out the Google Node.js API Client library.
Quick Start
We recommend installing the individual packages that you need, which are provided under the @google-cloud
namespace. For example:
$ npm install --save @google-cloud/datastore
$ npm install --save @google-cloud/storage
var config = {
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
};
var datastore = require('@google-cloud/datastore')(config);
var storage = require('@google-cloud/storage')(config);
The google-cloud meta-package
We also provide a meta-package, google-cloud
, which provides all of the individual APIs. However, in order to keep file size and memory use low, the use of this package is not recommended.
If you want the kitchen sink, however, get it with:
$ npm install --save google-cloud
var gcloud = require('google-cloud')({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var datastore = gcloud.datastore();
var storage = gcloud.storage();
Example Applications
- nodejs-getting-started - A sample and tutorial that demonstrates how to build a complete web application using Cloud Datastore, Cloud Storage, and Cloud Pub/Sub and deploy it to Google App Engine or Google Compute Engine.
- gcloud-node-todos - A TodoMVC backend using google-cloud-node and Datastore.
- gitnpm - Easily lookup an npm package's GitHub repo using google-cloud-node and Google App Engine.
- gcloud-kvstore - Use Datastore as a simple key-value store.
- hya-wave - Cloud-based web sample editor. Part of the hya-io family of products.
- gstore-node - Google Datastore Entities Modeling library.
- gstore-api - REST API builder for Google Datastore Entities.
Authentication
With google-cloud
it's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Cloud services.
On Google Cloud Platform
If you are running this client on Google Cloud Platform, we handle authentication for you with no configuration. You just need to make sure that when you set up the GCE instance, you add the correct scopes for the APIs you want to access.
var storage = require('@google-cloud/storage')();
var gcloud = require('google-cloud');
var storage = gcloud.storage();
Elsewhere
If you are not running this client on Google Cloud Platform, you need a Google Developers service account. To create a service account:
- Visit the Google Developers Console.
- Create a new project or click on an existing project.
- Navigate to APIs & auth > APIs section and turn on the following APIs (you may need to enable billing in order to use these services):
- BigQuery API
- Cloud Bigtable API
- Cloud Bigtable Admin API
- Cloud Bigtable Table Admin API
- Cloud Spanner API
- Google Cloud Datastore API
- Google Cloud DNS API
- Google Cloud Firestore API
- Google Cloud Natural Language API
- Google Cloud Pub/Sub API
- Google Cloud Resource Manager API
- Google Cloud Speech API
- Google Cloud Storage
- Google Cloud Storage JSON API
- Google Cloud Translation API
- Google Cloud Vision API
- Google Compute Engine API
- Stackdriver Logging API
- Navigate to APIs & auth > Credentials and then:
- If you want to use a new service account key, click on Create credentials and select Service account key. After the account key is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests.
- If you want to generate a new service account key for an existing service account, click on Generate new JSON key and download the JSON key file.
var projectId = process.env.GCLOUD_PROJECT;
var gcloud = require('google-cloud')({
projectId: projectId,
keyFilename: '/path/to/keyfile.json'
credentials: require('./path/to/keyfile.json')
key: '...'
});
You can also set auth on a per-API-instance basis. The examples below show you how.
Cloud Datastore (GA)
Follow the activation instructions to use the Cloud Datastore API with your project.
Using the Cloud Datastore API module
$ npm install --save @google-cloud/datastore
var datastore = require('@google-cloud/datastore');
Authentication
See Authentication.
Preview
var datastoreClient = datastore({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var key = datastoreClient.key(['Product', 'Computer']);
datastoreClient.get(key, function(err, entity) {
console.log(err || entity);
});
var blogPostData = {
title: 'How to make the perfect homemade pasta',
author: 'Andrew Chilton',
isDraft: true
};
var blogPostKey = datastoreClient.key('BlogPost');
datastoreClient.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
blogPostData.isDraft = false;
datastoreClient.save({
key: blogPostKey,
data: blogPostData
}, function(err) {
if (!err) {
}
});
});
Cloud Natural Language (GA)
Using the Natural Language API module
$ npm install --save @google-cloud/language
var language = require('@google-cloud/language');
Authentication
See Authentication.
Preview
var languageClient = language({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var content = 'Hello, world!';
var type = language.v1.types.Document.Type.PLAIN_TEXT;
var document = {
content : content,
type : type
};
languageClient.analyzeSentiment({document: document}).then(function(responses) {
var response = responses[0];
})
.catch(function(err) {
console.error(err);
});
Cloud Storage (GA)
Using the Cloud Storage API module
$ npm install --save @google-cloud/storage
var storage = require('@google-cloud/storage');
Authentication
See Authentication.
Preview
var fs = require('fs');
var gcs = storage({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
gcs.createBucket('my-new-bucket', function(err, bucket) {
if (!err) {
}
});
var bucket = gcs.bucket('my-existing-bucket');
bucket.upload('/photos/zoo/zebra.jpg', function(err, file) {
if (!err) {
}
});
bucket.file('giraffe.jpg').download({
destination: '/photos/zoo/giraffe.jpg'
}, function(err) {});
var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
var localWriteStream = fs.createWriteStream('/photos/zoo/giraffe.jpg');
remoteReadStream.pipe(localWriteStream);
var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
localReadStream.pipe(remoteWriteStream);
Cloud Translation API (GA)
Using the Google Translate API module
$ npm install --save @google-cloud/translate
var translate = require('@google-cloud/translate');
Authentication
See Authentication.
Preview
var translateClient = translate({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
translateClient.translate('Hello', 'es', function(err, translation) {
if (!err) {
}
});
translateClient.detect('Hello', function(err, results) {
if (!err) {
}
});
translateClient.getLanguages(function(err, languages) {
if (!err) {
}
});
Google Stackdriver Logging (GA)
Using the Google Stackdriver Logging API module
$ npm install --save @google-cloud/logging
var logging = require('@google-cloud/logging');
Authentication
See Authentication.
Preview
var loggingClient = logging({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var gcs = storage();
loggingClient.createSink('my-new-sink', {
destination: gcs.bucket('my-sink')
}, function(err, sink) {});
var syslog = loggingClient.log('syslog');
var metadata = {
resource: {
type: 'gce_instance',
labels: {
zone: 'global',
instance_id: '3'
}
}
};
var entry = syslog.entry(metadata, {
delegate: process.env.user
});
syslog.critical(entry, function(err) {});
loggingClient.getEntries(function(err, entries) {
if (!err) {
}
});
Cloud Firestore (Beta)
Using the Cloud Firestore API module
$ npm install --save @google-cloud/firestore
const Firestore = require('@google-cloud/firestore');
Authentication
See Authentication.
Preview
const firestore = new Firestore({
projectId: 'YOUR_PROJECT_ID',
keyFilename: '/path/to/keyfile.json',
});
const document = firestore.doc('posts/intro-to-firestore');
document.set({
title: 'Welcome to Firestore',
body: 'Hello World',
}).then(() => {
});
document.update({
body: 'My first Firestore app',
}).then(() => {
});
document.get().then(doc => {
});
document.delete().then(() => {
});
Cloud Pub/Sub (Beta)
Using the Cloud Pub/Sub API module
$ npm install --save @google-cloud/pubsub
var pubsub = require('@google-cloud/pubsub');
Authentication
See Authentication.
Preview
var pubsubClient = pubsub({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var topic = pubsubClient.topic('my-topic');
var publisher = topic.publisher();
var message = new Buffer('New message!');
publisher.publish(message, function(err, messageId) {});
topic.createSubscription('subscription-name', function(err, subscription) {
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
Cloud Spanner (GA)
Using the Cloud Spanner API module
$ npm install --save @google-cloud/spanner
var spanner = require('@google-cloud/spanner');
Authentication
See Authentication.
Preview
var spannerClient = spanner({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var instance = spannerClient.instance('my-instance');
var database = instance.database('my-database');
var schema = `
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
) PRIMARY KEY(SingerId)
`;
database.createTable(schema, function(err, table, operation) {
if (err) {
}
operation
.on('error', function(err) {})
.on('complete', function() {
});
});
var table = database.table('Singers');
table.insert({
SingerId: 10,
FirstName: 'Eddie',
LastName: 'Wilson'
}, function(err) {
if (!err) {
}
});
database.runStream('SELECT * FROM Singers')
.on('error', function(err) {})
.on('data', function(row) {
}
})
.on('end', function() {
});
Cloud Speech (Beta)
Using the Cloud Speech API module
$ npm install --save @google-cloud/speech
var speech = require('@google-cloud/speech');
Authentication
See Authentication.
Preview
var speechClient = speech({
projectId: 'my-project',
keyFilename: '/path/to/keyfile.json'
});
var languageCode = 'en-US';
var sampleRateHertz = 44100;
var encoding = speech.v1.types.RecognitionConfig.AudioEncoding.FLAC;
var config = {
languageCode : languageCode,
sampleRateHertz : sampleRateHertz,
encoding : encoding
};
var uri = 'gs://gapic-toolkit/hello.flac';
var audio = {
uri : uri
};
var request = {
config: config,
audio: audio
};
speechClient.recognize(request).then(function(responses) {
var response = responses[0];
})
.catch(function(err) {
console.error(err);
});
Cloud Vision (Beta)
Using the Cloud Vision API module
$ npm install --save @google-cloud/vision
var vision = require('@google-cloud/vision');
Authentication
See Authentication.
Preview
var visionClient = vision({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var gcsImageUri = 'gs://gapic-toolkit/President_Barack_Obama.jpg';
var source = {
gcsImageUri : gcsImageUri
};
var image = {
source : source
};
var type = vision.v1.types.Feature.Type.FACE_DETECTION;
var featuresElement = {
type : type
};
var features = [featuresElement];
var requestsElement = {
image : image,
features : features
};
var requests = [requestsElement];
visionClient.batchAnnotateImages({requests: requests}).then(function(responses) {
var response = responses[0];
})
.catch(function(err) {
console.error(err);
});
Google BigQuery (Beta)
Using the BigQuery API module
$ npm install --save @google-cloud/bigquery
var bigquery = require('@google-cloud/bigquery');
Authentication
See Authentication.
Preview
var bigqueryClient = bigquery({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var schoolsDataset = bigqueryClient.dataset('schools');
var schoolsTable = schoolsDataset.table('schoolsData');
schoolsTable.import('/local/file.json', function(err, job) {});
var job = bigqueryClient.job('job-id');
job.getQueryResults(function(err, rows) {});
job.getQueryResults().on('data', function(row) {});
Google Stackdriver Monitoring (Beta)
:warning: This is an auto-generated API
It does not follow the conventions you're familiar with from other parts of our library. A handwritten layer is not yet available.
The example below shows you how to instantiate the generated client. For further documentation, please browse the Monitoring .proto files on GitHub.
Using the Google Stackdriver Monitoring API module
$ npm install --save @google-cloud/monitoring
var monitoring = require('@google-cloud/monitoring');
Authentication
See Authentication.
Preview
var client = monitoring.metric({
});
var formattedName = client.projectPath(projectId);
client.listMonitoredResourceDescriptors({name: formattedName}).then(function(responses) {
var resources = responses[0];
for (var i = 0; i < resources.length; ++i) {
}
})
.catch(function(err) {
console.error(err);
});
var formattedName = client.projectPath(projectId);
var options = {autoPaginate: false};
function callback(responses) {
var resources = responses[0];
var nextRequest = responses[1];
for (var i = 0; i < resources.length; ++i) {
}
if (nextRequest) {
return client.listMonitoredResourceDescriptors(nextRequest, options).then(callback);
}
}
client.listMonitoredResourceDescriptors({name: formattedName}, options)
.then(callback)
.catch(function(err) {
console.error(err);
});
Cloud Bigtable (Alpha)
You may need to create an instance to use the Cloud Bigtable API with your project.
Using the Cloud Bigtable API module
$ npm install --save @google-cloud/bigtable
var bigtable = require('@google-cloud/bigtable');
Authentication
See Authentication.
Preview
var bigtableClient = bigtable({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var instance = bigtableClient.instance('my-instance');
var table = instance.table('prezzy');
table.getRows(function(err, rows) {});
var row = table.row('alincoln');
row.save('follows:gwashington', 1, function(err) {
if (err) {
}
row.get('follows:gwashington', function(err, data) {
if (err) {
}
});
});
Cloud DNS (Alpha)
Using the Cloud DNS API module
$ npm install --save @google-cloud/dns
var dns = require('@google-cloud/dns');
Authentication
See Authentication.
Preview
var dnsClient = dns({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
dnsClient.createZone('my-new-zone', {
dnsName: 'my-domain.com.'
}, function(err, zone) {});
var zone = dnsClient.zone('my-existing-zone');
var nsRecord = zone.record('ns', {
ttl: 86400,
name: 'my-domain.com.',
data: 'ns-cloud1.googledomains.com.'
});
zone.addRecords([nsRecord], function(err, change) {});
zone.export('/zonefile.zone', function(err) {});
Cloud Resource Manager (Alpha)
Using the Cloud Resource Manager API module
$ npm install --save @google-cloud/resource
var resource = require('@google-cloud/resource');
Authentication
See Authentication.
Preview
var resourceClient = resource({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
resourceClient.getProjects(function(err, projects) {
if (!err) {
}
});
var project = resourceClient.project();
project.getMetadata(function(err, metadata) {
});
Google Compute Engine (Alpha)
Using the Compute Engine API module
$ npm install --save @google-cloud/compute
var compute = require('@google-cloud/compute');
Authentication
See Authentication.
Preview
var gce = compute({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json'
});
var zone = gce.zone('us-central1-a');
var name = 'ubuntu-http';
zone.createVM(name, { os: 'ubuntu' }, function(err, vm, operation) {
operation
.on('error', function(err) {})
.on('running', function(metadata) {})
.on('complete', function(metadata) {
});
});
Google Stackdriver Debugger (Alpha)
The source code for the Node.js Cloud Debugger Agent lives in a separate repo.
Using the Stackdriver Debug Agent module
$ npm install --save @google-cloud/debug-agent
require('@google-cloud/debug-agent').start({ allowExpressions: true });
For more details on API usage, please see the Stackdriver Debug Agent Github Repository.
Google Stackdriver Error Reporting (Alpha)
Using the Stackdriver Error Reporting module
$ npm install --save @google-cloud/error-reporting
The module provides automatic uncaught exception handling, manual error reporting, and integration with common frameworks like express and hapi.
var errors = require('@google-cloud/error-reporting')();
Authentication
See Authentication.
Preview
errors.report(new Error('Something broke!'));
For more details on API usage, please see the documentation.
Google Stackdriver Trace (Alpha)
The source code for the Node.js Cloud Trace Agent lives in a separate repo.
Using the Stackdriver Trace Agent module
$ npm install --save @google-cloud/trace-agent
var trace = require('@google-cloud/trace-agent').start();
For more details on API usage, please see the Stackdriver Trace Agent Github Repository.
Versioning
This library follows Semantic Versioning.
Please note it is currently under active development. Any release versioned 0.x.y
is subject to backwards-incompatible changes at any time.
GA: Libraries defined at the GA (general availability) quality level are stable. The code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against GA libraries are addressed with the highest priority.
Please note that the auto-generated portions of the GA libraries (the ones in modules such as v1
or v2
) are considered to be of Beta quality, even if the libraries that wrap them are GA.
Beta: Libraries defined at the Beta quality level are expected to be mostly stable, while we work towards their release candidate. We will address issues and requests with a higher priority.
Alpha: Libraries defined at the Alpha quality level are still a work-in-progress and are more likely to get backwards-incompatible updates.
Contributing
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING for more information on how to get started.
License
Apache 2.0 - See LICENSE for more information.