Get an OAuth2 access token for the Firebase Cloud Messaging HTTP v1 API.
This project uses pnpm. Install it once if you don't have it:
npm install -g pnpm
# or: brew install pnpmThen install dependencies:
pnpm install-
Copy the example env file:
cp .env.example .env
-
Fill in the two values in
.env— see the guides below.
- Open the Firebase console.
- Select your project (or create one).
- Click the gear icon next to Project Overview → Project settings.
- On the General tab, look for Project ID under "Your project".
- Direct link:
https://console.firebase.google.com/project/_/settings/general - The ID is usually a lowercase slug like
my-app-12345, not the human-readable display name.
- Direct link:
- Paste it into
.envasPROJECT_ID=my-app-12345.
- In the Firebase console, go to Project settings → Service accounts.
- Direct link:
https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Direct link:
- Click Generate new private key, then Generate key.
- Save the downloaded JSON file somewhere safe (e.g.
./service-account.jsonin this folder). - Set
GOOGLE_APPLICATION_CREDENTIALSin.envto the path of that file.
Security: the service-account JSON grants admin access to your Firebase project. Never commit it to git. Add it to
.gitignoreand rotate the key in the Firebase console if it has been exposed.
pnpm start
# or: node firebase-auth-server.jsThe script prints the access token to stdout. Use it as a Bearer token when calling the FCM HTTP v1 send endpoint:
POST https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send
Authorization: Bearer <access_token>
Access tokens expire after 1 hour — rerun the script to get a fresh one.