The documentation you are viewing is for Dapr v1.4 which is an older version of Dapr. For up-to-date documentation, see the latest version.

Use the Dapr API

After running the dapr init command in the previous step, your local environment has the Dapr sidecar binaries as well as default component definitions for both state management and a message broker (both using Redis). You can now try out some of what Dapr has to offer by using the Dapr CLI to run a Dapr sidecar and try out the state API that will allow you to store and retrieve a state. You can learn more about the state building block and how it works in these docs.

You will now run the sidecar and call the API directly (simulating what an application would do).

Step 1: Run the Dapr sidecar

One of the most useful Dapr CLI commands is dapr run. This command launches an application together with a sidecar. For the purpose of this tutorial you’ll run the sidecar without an application.

Run the following command to launch a Dapr sidecar that will listen on port 3500 for a blank application named myapp:

dapr run --app-id myapp --dapr-http-port 3500

With this command, no custom component folder was defined, so Dapr uses the default component definitions that were created during the init flow (these can be found under $HOME/.dapr/components on Linux or MacOS and under %USERPROFILE%\.dapr\components on Windows). These tell Dapr to use the local Redis Docker container as a state store and message broker.

Step 2: Save state

We will now update the state with an object. The new state will look like this:

[
  {
    "key": "name",
    "value": "Bruce Wayne"
  }
]

Notice, the object contained in the state has a key assigned with the value name. You will use the key in the next step.

Run the command shown below to store the new state.


curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{ "key": "name", "value": "Bruce Wayne"}]' -Uri 'http://localhost:3500/v1.0/state/statestore'

Step 3: Get state

Now get the object you just stored in the state by using the state management API with the key name:


With the same Dapr instance running from above run:

curl http://localhost:3500/v1.0/state/statestore/name

With the same Dapr instance running from above run:

Invoke-RestMethod -Uri 'http://localhost:3500/v1.0/state/statestore/name'

Step 4: See how the state is stored in Redis

You can look in the Redis container and verify Dapr is using it as a state store. Run the following to use the Redis CLI:

docker exec -it dapr_redis redis-cli

List the redis keys to see how Dapr created a key value pair (with the app-id you provided to dapr run as a prefix to the key):

keys *
1) "myapp||name"

View the state value by running:

hgetall "myapp||name"
1) "data"
2) "\"Bruce Wayne\""
3) "version"
4) "1"

Exit the redis-cli with:

exit
Next step: Define a component >>

Last modified November 18, 2021 : Merge pull request #1978 from tanvigour/v1.4 (85161cd)