Test functions interactively

The Cloud Functions shell provides an interactive shell for invokingfunctions with test data. The shell supports all trigger types.

Set up admin credentials (optional)

If you want your functions tests to interact with Google APIs or other FirebaseAPIs via theFirebase Admin SDK, you may need to set upadmin credentials.

  • Cloud Firestore andRealtime Database triggers already have sufficient credentials, and donot require additional setup.
  • All other APIs, including Firebase APIs such asAuthentication andFCM or Google APIs such as Cloud Translation or Cloud Speech, require the setup steps described in this section. This applies whether you're using theCloud Functions shell orfirebase emulators:start.

To set up admin credentials for emulated functions:

  1. Open theService Accounts paneof theGoogle Cloud console.
  2. Make sure thatApp Engine default service account is selected, and usethe options menu at right to selectCreate key.
  3. When prompted, selectJSON for the key type, and clickCreate.
  4. Set your Google default credentials to point to the downloaded key:

    Unix

    export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"firebase functions:shell

    Windows

    set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.jsonfirebase functions:shell

After completing these steps, your functions tests can access Firebase andGoogle APIs using theAdmin SDK. For example, when testinganAuthentication trigger, the emulated function could calladmin.auth().getUserByEmail(email).

Serve functions using a Cloud Functions shell

The Cloud Functions shell emulates all types of function triggers with aninteractive shell for invoking the functions with test data. Options varyby function type, but the basic usage format is:

myFunctionName(data, options)

Thedata parameter is required for Realtime Database, Cloud Firestore,and PubSub triggers, and optional for all other function types.Also, the optionaloptions parameter is valid only for Realtime Databaseand Cloud Firestore functions.

Optionally, you can load test data from a localfile by saving the file as a variable and invoking a function with it:

vardata=require('./path/to/testData.json');myFunction(data);

Install and configure the Cloud Functions shell

To use this feature,firebase-tools must have minimum version 3.11.0, andfirebase-functions SDK must have minimum version 0.6.2. To update both,run the following commands in thefunctions/ directory for your project:

npminstall--savefirebase-functions@latestnpminstall-gfirebase-tools

If you're using custom functions configuration variables, first run thecommand to get your custom config (run this within thefunctions directory)in your local environment:

firebase functions:config:get > .runtimeconfig.json# If using Windows PowerShell, replace the above with:# firebase functions:config:get | ac .runtimeconfig.json

Finally, run the shell with the following command:

firebase functions:shell

Invoke HTTPS functions

For invoking HTTPS functions in the shell, usage is the same as therequest NPM module, but replacerequest with the name of the function you want to emulate. For example:

# invokemyHttpsFunction()myHttpsFunction.get()myHttpsFunction.post()# invoke at sub-pathmyHttpsFunction('/path')myHttpsFunction.get('/path')myHttpsFunction.post('/path')# send POST request with form datamyHttpsFunction.post('/path').form( {foo: 'bar' })

Invoke HTTPS Callable functions

When invoking HTTPS Callable functions locally, you'll need to provide appropriate test data.

# invokemyCallableFunction('test data')myCallableFunction({'foo': 'bar'})

Optionally, you may pass in aFirebase-Instance-ID-token as the second parameter. This must be a string.

#invokewithFCMregistrationtokenmyCallableFunction('testdata',{instanceIdToken:'sampletoken'})

Emulation ofcontext.auth is currently unavailable.

Invoke Realtime Database functions

When running Realtime Database functions locally, you'll need to provideappropriate test data. This generally means providing new test data foronCreate operations, old/removed data foronDelete operations, and both foronUpdate oronWrite functions:

# invoke onCreate functionmyDatabaseFunction('new_data')# invoke onDelete functionmyDatabaseFunction('old_data')# invoke onUpdate or onWrite functionmyDatabaseFunction({before: 'old_data', after: 'new_data' })

In addition to thebefore/after options, the shell provides theparamsoption to use in mocking wildcards in a path:

# mock wildcards in path, for example: if the path was input/{group}/{id}myDatabaseFunction('data', {params: {group: 'a', id: 123}})

By default, the shell runs Realtime Database functions with admin (service account)privileges. Use theauth option to instead run functions as a particularend user, or as an unauthenticated user:

# to mock unauthenticated usermyDatabaseFunction('data', {authMode: 'USER'})# to mock end usermyDatabaseFunction('data', {auth: {uid: 'abcd'}})

Invoke Firestore functions

When running Firestore functions locally, you'll need to provideappropriate test data. This generally means providing new test data foronCreate operations, old/removed data foronDelete operations, and both foronUpdate oronWrite functions. Note that Firestore data has to bekey-value pairs; seeSupported Data Types.

# invoke onCreate functionmyFirestoreFunction({foo: ‘new’})# invoke onDelete functionmyFirestoreFunction({foo: ‘old’})# invoke onUpdate or onWrite functionmyFirestoreFunction({before: {foo: ‘old’}, after: {foo: ‘new’} })

In addition to thebefore/after fields of thedata object,you can use theparams fields on theoptions object to mockwildcards in a document name:

# mock wildcards in document name, for example: if the name was input/{group}/{id}myFirestoreFunction({foo: ‘new’}, {params: {group: 'a', id: 123}})

The shell always runs Firestore functions with administrative privileges, whichmeans it mocks a create/update/delete event as if it were done by anadministrative user.

Invoke PubSub functions

For PubSub functions, insert your message payload in aBuffer instance andadd optionally data attributes as shown:

// invokes a function with the JSON message { hello: 'world' } and attributes { foo: 'bar' }myPubsubFunction({data:newBuffer('{"hello":"world"}'),attributes:{foo:'bar'}})

Invoke Analytics functions

You can invoke an Analytics function without any data byrunningmyAnalyticsFunction() in the shell.To run the function with test data, it is recommended to define a variable forthe specific event data fields that your function needs:

vardata={eventDim:[{//populatesevent.data.paramsparams:{foo:{stringValue:'bar'}},//Alsovalid://{intValue:'10'},{floatValue:'1.0'},{doubleValue:'1.0'}//populatesevent.data.namename:'event_name',//populatesevent.data.logTime,specifyinmicrosecondstimestampMicros:Date.now()*1000,//populatesevent.data.previousLogTime,specifyinmicrosecondspreviousTimestampMicros:Date.now()*1000,//populatesevent.data.reportingDate,specifyin'YYYYMMDD'formatdate:'20170930',//populatesevent.data.valueInUSDvalueInUsd:230}],userDim:userDim};myAnalyticsFunction(data);

Invoke Storage and Auth functions

For Storage and Auth functions, invoke the local function with thetest data that you’d like to see inside of the function. Your test data mustfollow the corresponding data formats:

Specify only the fields that your code depends on, or none at all if you onlywant to run the function.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-06 UTC.