Invoke Functions Locally

Functions can be invoked locally for development and testing purposes. Local Invocations don’t impact Functions Limits.

You can run and invoke your function locally using the Salesforce CLI or VS Code. Before invoking locally, make sure you've created a scratch org with Salesforce Functions enabled, as described in Develop Functions Using Scratch Orgs. Also make sure your Functions permission set is configured properly as described in Function Permissions.

You can only have one function running locally at a time.

Start the Function Locally Using Salesforce CLI

Navigate to the function directory and build and run your function:

In VS Code, open index.js. From the Command Palette, select Run SFDX: Start Function. Output from the function prints to the Output panel in VS Code.

When the function is running, create or open a payload JSON file and click Invoke in the editor view to invoke the currently running function with the payload file content.

VS Code Send Request Screenshot

Start the Local Function Using Container

Alternatively build and run your function locally in a container, like Docker. Make sure your container software is running, then run:

In VS Code, open index.js. From the Command Palette, select Run SFDX: Start Function with Container. Output from the function prints to the Output panel in VS Code.

When the function is running, create or open a payload JSON file and click Invoke in the editor view.

The function is ready to receive requests when you see Starting {FunctionName}. Leave the terminal window running and use a new terminal window for the next step.

To test a running function, send it a JSON payload. Pass the payload inline or create a payload.json file in your function directory:

Navigate to your project root directory and invoke the function:

On Windows, remove the single quotes:

sf run function -l http://localhost:8080 -p @functions/myfunction/payload.json

This command invokes the function running on localhost port 8080 (the default for locally running functions), with the payload JSON file. You see output similar to:

The execution logs stream in the terminal running the function. The output looks something like this:

To invoke the function with an inline payload:

On Windows, escape any double-quote marks (“) in the payload with a backslash (”\“). For example:

Have your locally running function connect to the scratch org by providing the scratch org alias with the -o argument.

You often set your scratch org alias when you create your scratch org, as described in Develop Functions Using Scratch Orgs. Visit Create a Scratch Org for help on creating and configuring your scratch org.

If you want to invoke your function and debug in VS Code, use Debug Invoke to invoke the function and attach the VS Code debugger. See Test and Validate Function - Debug Using VS Code for details on debugging using VS Code.

The function continues to run and waits for requests until you stop the function. Stop the running function by pressing CTRL-C in the terminal where your function is running.

In VS Code, use SFDX: Stop Function to stop any currently running Function.