---
schema_version: "1"
id: 9228508043c8e99bfde1445194c4be269e8e7fd4f8333f089b9cf377f135e323
title: Test a Function Locally
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforce-functions/functions/guide/quick-start/test-local-function.md
canonical_url: https://developer.salesforce.com/docs/platform/functions/guide/test-local-function.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 0229c2dea880f31cf7be3fde8d6af48423fcbaf115476d6b1cef9c7df4939ccf
products:
  - Salesforce Functions
---
# Test a Function Locally

Test functions in your local environment before you deploy to a compute environment. Build and run your function with the Salesforce CLI and invoke it locally.

**note:** Previously, local Salesforce Functions development required using a container like Docker. Containers are no longer required to run a function locally. Learn more about [Local Function Invocations](https://developer.salesforce.com/docs/platform/functions/dev-guide/invoke-local.md).

## Build and Run the Function Locally

Navigate to the function directory and build and run your function:

```sh
sf run function start
```

The function is ready to receive requests when you see `Starting myfunction`. The function continues to run and waits for requests until you use Ctrl-C to stop. Leave the terminal window running and open a new terminal window for the next step.

## Invoke the Function

To test a running function, send it a JSON payload. Create a `payload.json` file in your function directory:

```json
{
  "name": "MyAccount",
  "accountNumber": "123456789",
  "industry": "Technology",
  "type": "Prospect",
  "website": "www.salesforce.com"
}
```

Navigate to your project root directory and invoke the function, which is now running on your localhost:

```sh
sf run function -l http://localhost:8080 -p '@functions/myfunction/payload.json'
```

- `-l` running function URL
- `-p` the payload to send to the function

This command invokes the function running on localhost port 8080 (the default for locally running functions) with the payload JSON file. The output looks something like:

```sh
Using defaultusername MyScratchOrgAlias login credential to initialize context
POST http://localhost:8080... 200
{
  "done": true,
  "totalSize": 1,
  "records": [
    {
      "type": "Account",
      "fields": {
        "id": "00163000012fAD0AAM",
        "isdeleted": false,
        "masterrecordid": null,
        "name": "MyAccount-1623718504016",

        ...fields omitted...

        "yearstarted": null,
        "sicdesc": null,
        "dandbcompanyid": null,
        "operatinghoursid": null
      }
    }
  ]
}
```

The execution logs stream in the terminal running the function (where you ran `sf run function start`). The output looks something like this:

```sh
{"level":30,"time":1623718504015,"pid":1,"hostname":"39f3353de3f7",
"invocationId":"a9c869ab-2130-42e0-9615-b342c8b97bd1","msg":"Invoking
 Myfunction with payload {\"name\":\"MyAccount\",
 \"accountNumber\":\"123456789\",\"industry\":\"Technology\",
 \"type\":\"Prospect\",\"website\":\"www.salesforce.com\"}"}
```

## Stop the Function

To stop the function, enter Ctrl-C in the terminal window where you ran `sf run function start`.
