Node.js Google Authentication Service Account Tokens
This is a low level utility library used to interact with Google Authentication services.In most cases, you probably want to use thegoogle-auth-libraryinstead.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-09 UTC."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [8.0.0 (latest)](/nodejs/docs/reference/gtoken/latest)\n- [7.1.0](/nodejs/docs/reference/gtoken/7.1.0)\n- [7.0.1](/nodejs/docs/reference/gtoken/7.0.1)\n- [6.1.2](/nodejs/docs/reference/gtoken/6.1.2)\n- [5.3.2](/nodejs/docs/reference/gtoken/5.3.2) \n\n[node-gtoken](https://github.com/googleapis/node-gtoken)\n========================================================\n\n[](https://npmjs.org/package/gtoken)\n[](https://snyk.io/test/github/googleapis/node-gtoken)\n[](https://codecov.io/gh/googleapis/node-gtoken)\n[](https://www.npmjs.com/package/gts)\n\u003e Node.js Google Authentication Service Account Tokens\n\nThis is a low level utility library used to interact with Google Authentication services. **In most cases, you probably want to use the [google-auth-library](https://github.com/googleapis/google-auth-library-nodejs) instead.**\n\n- [gtoken API Reference](https://googleapis.dev/nodejs/gtoken/latest/)\n- [github.com/googleapis/node-gtoken](https://github.com/googleapis/node-gtoken)\n\nInstallation\n------------\n\n npm install gtoken\n\nUsage\n-----\n\n### Use with a `.pem` or `.json` key file:\n\n const { GoogleToken } = require('https://cloud.google.com/nodejs/docs/reference/gtoken/latest/overview.html');\n const gtoken = new https://cloud.google.com/nodejs/docs/reference/gtoken/latest/gtoken/googletoken.html({\n keyFile: 'path/to/key.pem', // or path to .json key file\n email: 'my_service_account_email@developer.gserviceaccount.com',\n scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n eagerRefreshThresholdMillis: 5 * 60 * 1000\n });\n\n https://cloud.google.com/nodejs/docs/reference/gtoken/latest/overview.html.https://cloud.google.com/nodejs/docs/reference/gtoken/latest/gtoken/googletoken.html((err, tokens) =\u003e {\n if (err) {\n console.log(err);\n return;\n }\n console.log(tokens);\n // {\n // access_token: 'very-secret-token',\n // expires_in: 3600,\n // token_type: 'Bearer'\n // }\n });\n\nYou can also use the async/await style API: \n\n const tokens = await gtoken.getToken()\n console.log(tokens);\n\nOr use promises: \n\n gtoken.getToken()\n .then(tokens =\u003e {\n console.log(tokens)\n })\n .catch(console.error);\n\n### Use with a service account `.json` key file:\n\n const { GoogleToken } = require('https://cloud.google.com/nodejs/docs/reference/gtoken/latest/overview.html');\n const gtoken = new https://cloud.google.com/nodejs/docs/reference/gtoken/latest/gtoken/googletoken.html({\n keyFile: 'path/to/key.json',\n scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n eagerRefreshThresholdMillis: 5 * 60 * 1000\n });\n\n https://cloud.google.com/nodejs/docs/reference/gtoken/latest/overview.html.https://cloud.google.com/nodejs/docs/reference/gtoken/latest/gtoken/googletoken.html((err, tokens) =\u003e {\n if (err) {\n console.log(err);\n return;\n }\n console.log(tokens);\n });\n\n### Pass the private key as a string directly:\n\n const key = '-----BEGIN RSA PRIVATE KEY-----\\nXXXXXXXXXXX...';\n const { GoogleToken } = require('https://cloud.google.com/nodejs/docs/reference/gtoken/latest/overview.html');\n const gtoken = new https://cloud.google.com/nodejs/docs/reference/gtoken/latest/gtoken/googletoken.html({\n email: 'my_service_account_email@developer.gserviceaccount.com',\n scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n key: key,\n eagerRefreshThresholdMillis: 5 * 60 * 1000\n });\n\nOptions\n-------\n\n\u003e Various options that can be set when creating initializing the `gtoken` object.\n\n- `options.email or options.iss`: The service account email address.\n- `options.scope`: An array of scope strings or space-delimited string of scopes.\n- `options.sub`: The email address of the user requesting delegated access.\n- `options.keyFile`: The filename of `.json` key or `.pem` key.\n- `options.key`: The raw RSA private key value, in place of using `options.keyFile`.\n- `options.additionalClaims`: Additional claims to include in the JWT when requesting a token.\n- `options.eagerRefreshThresholdMillis`: How long must a token be valid for in order to return it from the cache. Defaults to 0.\n\n### .getToken(callback)\n\nReturns the cached tokens or requests a new one and returns it. \n\n gtoken.getToken((err, token) =\u003e {\n console.log(err || token);\n // gtoken.rawToken value is also set\n });\n\n### .getCredentials('path/to/key.json')\n\nGiven a keyfile, returns the key and (if available) the client email. \n\n const creds = await gtoken.getCredentials('path/to/key.json');\n\n### Properties\n\n\u003e Various properties set on the gtoken object after call to `.getToken()`.\n\n- `gtoken.idToken`: The OIDC token returned (if any).\n- `gtoken.accessToken`: The access token.\n- `gtoken.expiresAt`: The expiry date as milliseconds since 1970/01/01\n- `gtoken.key`: The raw key value.\n- `gtoken.rawToken`: Most recent raw token data received from Google.\n\n### .hasExpired()\n\nReturns true if the token has expired, or token does not exist. \n\n const tokens = await gtoken.getToken();\n gtoken.hasExpired(); // false\n\n### .revokeToken()\n\nRevoke the token if set. \n\n await gtoken.revokeToken();\n console.log('Token revoked!');\n\nDownloading your private `.json` key from Google\n------------------------------------------------\n\n1. Open the [Google Developer Console](https://console.developers.google.com).\n2. Open your project and under \"APIs \\& auth\", click Credentials.\n3. Generate a new `.json` key and download it into your project.\n\nConverting your `.p12` key to a `.pem` key\n------------------------------------------\n\nIf you'd like to convert to a `.pem` for use later, use OpenSSL if you have it installed. \n\n $ openssl pkcs12 -in key.p12 -nodes -nocerts \u003e key.pem\n\nDon't forget, the passphrase when converting these files is the string `'notasecret'`\n\nLicense\n-------\n\n[MIT](https://github.com/googleapis/node-gtoken/blob/main/LICENSE)"]]