Client

Introduction

The Dapr Client allows you to communicate with the Dapr Sidecar and get access to its client facing features such as Publishing Events, Invoking Output Bindings, State Management, Secret Management, and much more.

Pre-requisites

Installing and importing Dapr’s JS SDK

  1. Install the SDK with npm:
  1. npm i @dapr/dapr --save
  1. Import the libraries:
  1. import { DaprClient, DaprServer, HttpMethod, CommunicationProtocolEnum } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1"; // Dapr Sidecar Host
  3. const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
  4. const serverHost = "127.0.0.1"; // App Host of this Example Server
  5. const serverPort = "50051"; // App Port of this Example Server
  6. // HTTP Example
  7. const client = new DaprClient({ daprHost, daprPort });
  8. // GRPC Example
  9. const client = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocolEnum.GRPC });

Running

To run the examples, you can use two different protocols to interact with the Dapr sidecar: HTTP (default) or gRPC.

Using HTTP (default)

  1. import { DaprClient } from "@dapr/dapr";
  2. const client = new DaprClient({ daprHost, daprPort });
  1. # Using dapr run
  2. dapr run --app-id example-sdk --app-protocol http -- npm run start
  3. # or, using npm script
  4. npm run start:dapr-http

Using gRPC

Since HTTP is the default, you will have to adapt the communication protocol to use gRPC. You can do this by passing an extra argument to the client or server constructor.

  1. import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
  2. const client = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocol.GRPC });
  1. # Using dapr run
  2. dapr run --app-id example-sdk --app-protocol grpc -- npm run start
  3. # or, using npm script
  4. npm run start:dapr-grpc

Environment Variables

Dapr Sidecar Endpoints

You can use the DAPR_HTTP_ENDPOINT and DAPR_GRPC_ENDPOINT environment variables to set the Dapr Sidecar’s HTTP and gRPC endpoints respectively. When these variables are set, the daprHost and daprPort don’t have to be set in the options argument of the constructor, the client will parse them automatically out of the provided endpoints.

  1. import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
  2. // Using HTTP, when DAPR_HTTP_ENDPOINT is set
  3. const client = new DaprClient();
  4. // Using gRPC, when DAPR_GRPC_ENDPOINT is set
  5. const client = new DaprClient({ communicationProtocol: CommunicationProtocol.GRPC });

If the environment variables are set, but daprHost and daprPort values are passed to the constructor, the latter will take precedence over the environment variables.

Dapr API Token

You can use the DAPR_API_TOKEN environment variable to set the Dapr API token. When this variable is set, the daprApiToken doesn’t have to be set in the options argument of the constructor, the client will get it automatically.

General

Increasing Body Size

You can increase the body size that is used by the application to communicate with the sidecar by using aDaprClient’s option.

  1. import { DaprClient, CommunicationProtocol } from "@dapr/dapr";
  2. // Allow a body size of 10Mb to be used
  3. // The default is 4Mb
  4. const client = new DaprClient({
  5. daprHost,
  6. daprPort,
  7. communicationProtocol: CommunicationProtocol.HTTP,
  8. maxBodySizeMb: 10,
  9. });

Proxying Requests

By proxying requests, we can utilize the unique capabilities that Dapr brings with its sidecar architecture such as service discovery, logging, etc., enabling us to instantly “upgrade” our gRPC services. This feature of gRPC proxying was demonstrated in community call 41.

Creating a Proxy

To perform gRPC proxying, simply create a proxy by calling the client.proxy.create() method:

  1. // As always, create a client to our dapr sidecar
  2. // this client takes care of making sure the sidecar is started, that we can communicate, ...
  3. const clientSidecar = new DaprClient({ daprHost, daprPort, communicationProtocol: CommunicationProtocol.GRPC });
  4. // Create a Proxy that allows us to use our gRPC code
  5. const clientProxy = await clientSidecar.proxy.create<GreeterClient>(GreeterClient);

We can now call the methods as defined in our GreeterClient interface (which in this case is from the Hello World example)

Behind the Scenes (Technical Working)

Architecture

  1. The gRPC service gets started in Dapr. We tell Dapr which port this gRPC server is running on through --app-port and give it a unique Dapr app ID with --app-id <APP_ID_HERE>
  2. We can now call the Dapr Sidecar through a client that will connect to the Sidecar
  3. Whilst calling the Dapr Sidecar, we provide a metadata key named dapr-app-id with the value of our gRPC server booted in Dapr (e.g. server in our example)
  4. Dapr will now forward the call to the gRPC server configured

Building blocks

The JavaScript Client SDK allows you to interface with all of the Dapr building blocks focusing on Client to Sidecar features.

Invocation API

Invoke a Service

  1. import { DaprClient, HttpMethod } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient({ daprHost, daprPort });
  6. const serviceAppId = "my-app-id";
  7. const serviceMethod = "say-hello";
  8. // POST Request
  9. const response = await client.invoker.invoke(serviceAppId, serviceMethod, HttpMethod.POST, { hello: "world" });
  10. // POST Request with headers
  11. const response = await client.invoker.invoke(
  12. serviceAppId,
  13. serviceMethod,
  14. HttpMethod.POST,
  15. { hello: "world" },
  16. { headers: { "X-User-ID": "123" } },
  17. );
  18. // GET Request
  19. const response = await client.invoker.invoke(serviceAppId, serviceMethod, HttpMethod.GET);
  20. }
  21. start().catch((e) => {
  22. console.error(e);
  23. process.exit(1);
  24. });

For a full guide on service invocation visit How-To: Invoke a service.

State Management API

Save, Get and Delete application state

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient({ daprHost, daprPort });
  6. const serviceStoreName = "my-state-store-name";
  7. // Save State
  8. const response = await client.state.save(
  9. serviceStoreName,
  10. [
  11. {
  12. key: "first-key-name",
  13. value: "hello",
  14. metadata: {
  15. foo: "bar",
  16. },
  17. },
  18. {
  19. key: "second-key-name",
  20. value: "world",
  21. },
  22. ],
  23. {
  24. metadata: {
  25. ttlInSeconds: "3", // this should override the ttl in the state item
  26. },
  27. },
  28. );
  29. // Get State
  30. const response = await client.state.get(serviceStoreName, "first-key-name");
  31. // Get Bulk State
  32. const response = await client.state.getBulk(serviceStoreName, ["first-key-name", "second-key-name"]);
  33. // State Transactions
  34. await client.state.transaction(serviceStoreName, [
  35. {
  36. operation: "upsert",
  37. request: {
  38. key: "first-key-name",
  39. value: "new-data",
  40. },
  41. },
  42. {
  43. operation: "delete",
  44. request: {
  45. key: "second-key-name",
  46. },
  47. },
  48. ]);
  49. // Delete State
  50. const response = await client.state.delete(serviceStoreName, "first-key-name");
  51. }
  52. start().catch((e) => {
  53. console.error(e);
  54. process.exit(1);
  55. });

For a full list of state operations visit How-To: Get & save state.

Query State API

  1. import { DaprClient } from "@dapr/dapr";
  2. async function start() {
  3. const client = new DaprClient({ daprHost, daprPort });
  4. const res = await client.state.query("state-mongodb", {
  5. filter: {
  6. OR: [
  7. {
  8. EQ: { "person.org": "Dev Ops" },
  9. },
  10. {
  11. AND: [
  12. {
  13. EQ: { "person.org": "Finance" },
  14. },
  15. {
  16. IN: { state: ["CA", "WA"] },
  17. },
  18. ],
  19. },
  20. ],
  21. },
  22. sort: [
  23. {
  24. key: "state",
  25. order: "DESC",
  26. },
  27. ],
  28. page: {
  29. limit: 10,
  30. },
  31. });
  32. console.log(res);
  33. }
  34. start().catch((e) => {
  35. console.error(e);
  36. process.exit(1);
  37. });

PubSub API

Publish messages

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient({ daprHost, daprPort });
  6. const pubSubName = "my-pubsub-name";
  7. const topic = "topic-a";
  8. // Publish message to topic as text/plain
  9. // Note, the content type is inferred from the message type unless specified explicitly
  10. const response = await client.pubsub.publish(pubSubName, topic, "hello, world!");
  11. // If publish fails, response contains the error
  12. console.log(response);
  13. // Publish message to topic as application/json
  14. await client.pubsub.publish(pubSubName, topic, { hello: "world" });
  15. // Publish a JSON message as plain text
  16. const options = { contentType: "text/plain" };
  17. await client.pubsub.publish(pubSubName, topic, { hello: "world" }, options);
  18. // Publish message to topic as application/cloudevents+json
  19. // You can also use the cloudevent SDK to create cloud events https://github.com/cloudevents/sdk-javascript
  20. const cloudEvent = {
  21. specversion: "1.0",
  22. source: "/some/source",
  23. type: "example",
  24. id: "1234",
  25. };
  26. await client.pubsub.publish(pubSubName, topic, cloudEvent);
  27. // Publish a cloudevent as raw payload
  28. const options = { metadata: { rawPayload: true } };
  29. await client.pubsub.publish(pubSubName, topic, "hello, world!", options);
  30. // Publish multiple messages to a topic as text/plain
  31. await client.pubsub.publishBulk(pubSubName, topic, ["message 1", "message 2", "message 3"]);
  32. // Publish multiple messages to a topic as application/json
  33. await client.pubsub.publishBulk(pubSubName, topic, [
  34. { hello: "message 1" },
  35. { hello: "message 2" },
  36. { hello: "message 3" },
  37. ]);
  38. // Publish multiple messages with explicit bulk publish messages
  39. const bulkPublishMessages = [
  40. {
  41. entryID: "entry-1",
  42. contentType: "application/json",
  43. event: { hello: "foo message 1" },
  44. },
  45. {
  46. entryID: "entry-2",
  47. contentType: "application/cloudevents+json",
  48. event: { ...cloudEvent, data: "foo message 2", datacontenttype: "text/plain" },
  49. },
  50. {
  51. entryID: "entry-3",
  52. contentType: "text/plain",
  53. event: "foo message 3",
  54. },
  55. ];
  56. await client.pubsub.publishBulk(pubSubName, topic, bulkPublishMessages);
  57. }
  58. start().catch((e) => {
  59. console.error(e);
  60. process.exit(1);
  61. });

Bindings API

Invoke Output Binding

Output Bindings

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient({ daprHost, daprPort });
  6. const bindingName = "my-binding-name";
  7. const bindingOperation = "create";
  8. const message = { hello: "world" };
  9. const response = await client.binding.send(bindingName, bindingOperation, message);
  10. }
  11. start().catch((e) => {
  12. console.error(e);
  13. process.exit(1);
  14. });

For a full guide on output bindings visit How-To: Use bindings.

Secret API

Retrieve secrets

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient({ daprHost, daprPort });
  6. const secretStoreName = "my-secret-store";
  7. const secretKey = "secret-key";
  8. // Retrieve a single secret from secret store
  9. const response = await client.secret.get(secretStoreName, secretKey);
  10. // Retrieve all secrets from secret store
  11. const response = await client.secret.getBulk(secretStoreName);
  12. }
  13. start().catch((e) => {
  14. console.error(e);
  15. process.exit(1);
  16. });

For a full guide on secrets visit How-To: Retrieve secrets.

Configuration API

Get Configuration Keys

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. async function start() {
  4. const client = new DaprClient({
  5. daprHost,
  6. daprPort: process.env.DAPR_GRPC_PORT,
  7. communicationProtocol: CommunicationProtocolEnum.GRPC,
  8. });
  9. const config = await client.configuration.get("config-store", ["key1", "key2"]);
  10. console.log(config);
  11. }
  12. start().catch((e) => {
  13. console.error(e);
  14. process.exit(1);
  15. });

Sample output:

  1. {
  2. items: {
  3. key1: { key: 'key1', value: 'foo', version: '', metadata: {} },
  4. key2: { key: 'key2', value: 'bar2', version: '', metadata: {} }
  5. }
  6. }

Subscribe to Configuration Updates

  1. import { DaprClient } from "@dapr/dapr";
  2. const daprHost = "127.0.0.1";
  3. async function start() {
  4. const client = new DaprClient({
  5. daprHost,
  6. daprPort: process.env.DAPR_GRPC_PORT,
  7. communicationProtocol: CommunicationProtocolEnum.GRPC,
  8. });
  9. // Subscribes to config store changes for keys "key1" and "key2"
  10. const stream = await client.configuration.subscribeWithKeys("config-store", ["key1", "key2"], async (data) => {
  11. console.log("Subscribe received updates from config store: ", data);
  12. });
  13. // Wait for 60 seconds and unsubscribe.
  14. await new Promise((resolve) => setTimeout(resolve, 60000));
  15. stream.stop();
  16. }
  17. start().catch((e) => {
  18. console.error(e);
  19. process.exit(1);
  20. });

Sample output:

  1. Subscribe received updates from config store: {
  2. items: { key2: { key: 'key2', value: 'bar', version: '', metadata: {} } }
  3. }
  4. Subscribe received updates from config store: {
  5. items: { key1: { key: 'key1', value: 'foobar', version: '', metadata: {} } }
  6. }

Cryptography API

Support for the cryptography API is only available on the gRPC client in the JavaScript SDK.

  1. import { createReadStream, createWriteStream } from "node:fs";
  2. import { readFile, writeFile } from "node:fs/promises";
  3. import { pipeline } from "node:stream/promises";
  4. import { DaprClient, CommunicationProtocolEnum } from "@dapr/dapr";
  5. const daprHost = "127.0.0.1";
  6. const daprPort = "50050"; // Dapr Sidecar Port of this example server
  7. async function start() {
  8. const client = new DaprClient({
  9. daprHost,
  10. daprPort,
  11. communicationProtocol: CommunicationProtocolEnum.GRPC,
  12. });
  13. // Encrypt and decrypt a message using streams
  14. await encryptDecryptStream(client);
  15. // Encrypt and decrypt a message from a buffer
  16. await encryptDecryptBuffer(client);
  17. }
  18. async function encryptDecryptStream(client: DaprClient) {
  19. // First, encrypt the message
  20. console.log("== Encrypting message using streams");
  21. console.log("Encrypting plaintext.txt to ciphertext.out");
  22. await pipeline(
  23. createReadStream("plaintext.txt"),
  24. await client.crypto.encrypt({
  25. componentName: "crypto-local",
  26. keyName: "symmetric256",
  27. keyWrapAlgorithm: "A256KW",
  28. }),
  29. createWriteStream("ciphertext.out"),
  30. );
  31. // Decrypt the message
  32. console.log("== Decrypting message using streams");
  33. console.log("Encrypting ciphertext.out to plaintext.out");
  34. await pipeline(
  35. createReadStream("ciphertext.out"),
  36. await client.crypto.decrypt({
  37. componentName: "crypto-local",
  38. }),
  39. createWriteStream("plaintext.out"),
  40. );
  41. }
  42. async function encryptDecryptBuffer(client: DaprClient) {
  43. // Read "plaintext.txt" so we have some content
  44. const plaintext = await readFile("plaintext.txt");
  45. // First, encrypt the message
  46. console.log("== Encrypting message using buffers");
  47. const ciphertext = await client.crypto.encrypt(plaintext, {
  48. componentName: "crypto-local",
  49. keyName: "my-rsa-key",
  50. keyWrapAlgorithm: "RSA",
  51. });
  52. await writeFile("test.out", ciphertext);
  53. // Decrypt the message
  54. console.log("== Decrypting message using buffers");
  55. const decrypted = await client.crypto.decrypt(ciphertext, {
  56. componentName: "crypto-local",
  57. });
  58. // The contents should be equal
  59. if (plaintext.compare(decrypted) !== 0) {
  60. throw new Error("Decrypted message does not match original message");
  61. }
  62. }
  63. start().catch((e) => {
  64. console.error(e);
  65. process.exit(1);
  66. });

For a full guide on cryptography visit How-To: Cryptography.

Distributed Lock API

Try Lock and Unlock APIs

  1. import { CommunicationProtocolEnum, DaprClient } from "@dapr/dapr";
  2. import { LockStatus } from "@dapr/dapr/types/lock/UnlockResponse";
  3. const daprHost = "127.0.0.1";
  4. const daprPortDefault = "3500";
  5. async function start() {
  6. const client = new DaprClient({ daprHost, daprPort });
  7. const storeName = "redislock";
  8. const resourceId = "resourceId";
  9. const lockOwner = "owner1";
  10. let expiryInSeconds = 1000;
  11. console.log(`Acquiring lock on ${storeName}, ${resourceId} as owner: ${lockOwner}`);
  12. const lockResponse = await client.lock.lock(storeName, resourceId, lockOwner, expiryInSeconds);
  13. console.log(lockResponse);
  14. console.log(`Unlocking on ${storeName}, ${resourceId} as owner: ${lockOwner}`);
  15. const unlockResponse = await client.lock.unlock(storeName, resourceId, lockOwner);
  16. console.log("Unlock API response: " + getResponseStatus(unlockResponse.status));
  17. }
  18. function getResponseStatus(status: LockStatus) {
  19. switch (status) {
  20. case LockStatus.Success:
  21. return "Success";
  22. case LockStatus.LockDoesNotExist:
  23. return "LockDoesNotExist";
  24. case LockStatus.LockBelongsToOthers:
  25. return "LockBelongsToOthers";
  26. default:
  27. return "InternalError";
  28. }
  29. }
  30. start().catch((e) => {
  31. console.error(e);
  32. process.exit(1);
  33. });

For a full guide on distributed locks visit How-To: Use Distributed Locks.

Workflow API

Workflow management

  1. import { DaprClient } from "@dapr/dapr";
  2. async function start() {
  3. const client = new DaprClient();
  4. // Start a new workflow instance
  5. const instanceId = await client.workflow.start("OrderProcessingWorkflow", {
  6. Name: "Paperclips",
  7. TotalCost: 99.95,
  8. Quantity: 4,
  9. });
  10. console.log(`Started workflow instance ${instanceId}`);
  11. // Get a workflow instance
  12. const workflow = await client.workflow.get(instanceId);
  13. console.log(
  14. `Workflow ${workflow.workflowName}, created at ${workflow.createdAt.toUTCString()}, has status ${
  15. workflow.runtimeStatus
  16. }`,
  17. );
  18. console.log(`Additional properties: ${JSON.stringify(workflow.properties)}`);
  19. // Pause a workflow instance
  20. await client.workflow.pause(instanceId);
  21. console.log(`Paused workflow instance ${instanceId}`);
  22. // Resume a workflow instance
  23. await client.workflow.resume(instanceId);
  24. console.log(`Resumed workflow instance ${instanceId}`);
  25. // Terminate a workflow instance
  26. await client.workflow.terminate(instanceId);
  27. console.log(`Terminated workflow instance ${instanceId}`);
  28. // Purge a workflow instance
  29. await client.workflow.purge(instanceId);
  30. console.log(`Purged workflow instance ${instanceId}`);
  31. }
  32. start().catch((e) => {
  33. console.error(e);
  34. process.exit(1);
  35. });