Use a Firebase Cloud Function to collect, log, and respond with the requestor's IP address and other meta info.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Use a Firebase Cloud Function to collect, log, and respond with the requestor's IP address and other meta info.


If an app requires logging a user's IP address for metrics, security logs, audit trails, etc, it can trigger this HTTP function via a GET request. The informtion can simply be console logged, saved to the database, or used in some other manner.



If a uid parameter is provided, the user's UID can be logged as well. This information could be passed in after successful authentication.


auth.onAuthStateChanged((user) => {
    .then((response) => {
       console.log('Response = ', response.json());

While perhaps not required in a real app, the function will return to the caller all available meta information. This can be modified as needed to suit any use case.


  "data": {
    "city": "dallas",
    "coordinates": "32.776664,-96.796988",
    "country": "US",
    "ipAddress": "",
    "rawHeaders": [
      "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0",
      "gzip, deflate, br",
    "region": "tx",
    "uid": "abc123",
    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0"
  "success": true


To use this HTTP function, setup as follows:

Firebase CLI

Install Firebase Tools globally

nvm use 8.12.0
npm install -g firebase-tools

Cloning and Configuring

git clone
cd firebase-log-ip-address
npm install
cd functions
npm install

NOTE: At this point, modify the .firebaserc file to point to the correct firebase project

Firebase Login

To use the Firebase CLI, you'll need to login.

firebase login

A browser will open asking for your login credentials (those used to access the Firebase console). Once logged in, you'll be able to perform other Firebase command line functions.


The HTTP function can be directly edited in the functions/src/index.ts file. Because the project uses TypeScript, the server will NOT detect changes to this file. In order for changes to take effect, run tsc in the functions directory after saving all changes. The server will then detect the changes.

Testing HTTP Functions

To test the getIpAddrInfo HTTP function, serve it via the local shell as follows:

cd functions
npm run serve

The emulator will provide an output that displays the local URL for each function:

> functions@ serve /Users/jn/Documents/Apps/get-ip-addr-info/functions
> npm run build && firebase serve --only functions

> functions@ build /Users/jn/Documents/Apps/get-ip-addr-info/functions
> tsc

=== Serving from '/Users/jn/Documents/Apps/get-ip-addr-info'...

i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.12.0 but Google Cloud Functions only supports v6.11.5.
✔  functions: getIpAddrInfo: http://localhost:5000/ip-address-info/us-central1/getIpAddrInfo

Using a browser, cURL, Paw, PostMan, or some other HTTP tool, generate a GET request to the URL provided in the emulator.


curl http://localhost:5000/ip-address-info/us-central1/getIpAddrInfo


NOTE: Deploying will trigger tslint verifications. If a function has code that does not meet the syntax requirements, it will be logged out and the deployment will not continue. All syntax validations must be fixed before deployment can be completed.

cd functions
npm run deploy