Firebase Admin Go SDK
repository·master·Indexed 23 days ago
https://github.com/firebase/firebase-admin-goThe Firebase Admin Go SDK allows Go applications running in privileged environments, such as servers and cloud functions, to interact with Firebase services. It provides clients for Auth, Database, Firestore, Storage, Messaging, App Check, Remote Config, Instance ID, and Phone Number Verification.
What's inside firebase-admin-go
- The Firebase Admin Go SDK allows you to access Firebase services from privileged environments (such as servers or cloud functions) using the Go programming language. Currently, the SDK provides support for Firebase custom authentication.
Install the Firebase Admin Go SDK
masterYou can install the Firebase Admin Go SDK using the
go getutility. You can either install the latest version or target a specific version using thev4module path.# Install the latest version: go get firebase.google.com/go/v4@latest # Or install a specific version: go get firebase.google.com/go/v4@4.x.xConfigure the Firebase App using Config
masterThe
Configstruct defines the settings used to initialize anApp. Use these fields to specify your project details and service endpoints.Field Type JSON Key Description ProjectIDstringprojectIdThe unique identifier for your Firebase project. DatabaseURLstringdatabaseURLThe URL of your Firebase Realtime Database. StorageBucketstringstorageBucketThe name of your Cloud Storage bucket. ServiceAccountIDstringserviceAccountIdThe ID of the service account to use. AuthOverride*map[string]interface{}databaseAuthVariableOverrideA map used to override database authentication variables. Supported Go versions
masterThe SDK follows the Go programming language release policy, supporting the two most-recent major Go releases. As of the current documentation, the supported versions are:
- Go 1.25
- Go 1.26
Initialize a Firebase App with NewApp
masterThe
NewAppfunction is the primary way to create anAppinstance, which serves as the central entity for accessing all Firebase services.Authentication:
- If
optscontains valid credentials (service account file, refresh token, or OAuth2 token source), the App uses them. - Otherwise, it attempts to use Google Application Default Credentials (ADC).
Configuration:
- If
configis provided, it uses the values in theConfigstruct. - If
configisnil, the SDK attempts to load configuration from theFIREBASE_CONFIGenvironment variable. This variable can be a JSON string or a path to a JSON file.
Project ID Resolution: If
config.ProjectIDis not set, the SDK resolves the Project ID by checking:- The credentials provided in
opts. - The
GOOGLE_CLOUD_PROJECTenvironment variable. - The
GCLOUD_PROJECTenvironment variable.
- If
Access Firebase services from an App instance
masterOnce an
Appis initialized, you can access various Firebase service clients using methods on theAppstruct. Most methods require acontext.Contextand return a client and an error.Available Service Clients:
Auth(ctx): Returns an*auth.Clientfor managing users and authentication.Database(ctx): Returns a*db.Clientfor the default Firebase Realtime Database.DatabaseWithURL(ctx, url): Returns a*db.Clientfor a specific database URL.Firestore(ctx): Returns a*firestore.Clientfor the default Firestore database.FirestoreWithDatabaseID(ctx, databaseID): Returns a*firestore.Clientfor a specific named Firestore database.Storage(ctx): Returns a*storage.Clientfor Cloud Storage.Messaging(ctx): Returns a*messaging.Clientfor Firebase Cloud Messaging.AppCheck(ctx): Returns an*appcheck.Clientfor App Check.RemoteConfig(ctx): Returns an*remoteconfig.Clientfor Remote Config.InstanceID(ctx): Returns an*iid.Clientfor Instance ID.PhoneNumberVerification(ctx): Returns a*phonenumberverification.Clientfor phone number verification.