Skip to content

Instantly share code, notes, and snippets.

Last active Mar 9, 2020
What would you like to do?
GCP Secret Manager to get runtime environment credentials
import {SecretManagerServiceClient} from '@google-cloud/secret-manager';
import * as admin from 'firebase-admin';
const client = new SecretManagerServiceClient();
let credentails:;
export const db = async (): Promise<> => {
if (credentails) {
return credentails;
} else {
const [version] = await client.accessSecretVersion({
name: <resource_id>, // enter the copied resource id here
const result: any = JSON.parse(;
const params = {
type: result.type,
projectId: result.project_id,
privateKeyId: result.private_key_id,
privateKey: result.private_key,
clientEmail: result.client_email,
clientId: result.client_id,
authUri: result.auth_uri,
tokenUri: result.token_uri,
authProviderX509CertUrl: result.auth_provider_x509_cert_url,
clientC509CertUrl: result.client_x509_cert_url,
credentails = admin.initializeApp({
credential: admin.credential.cert(params),
storageBucket: `gs://${result.project_id}`,
return credentails;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment