Client

Import the libraries:

  1. const daprHost = "127.0.0.1"; // Dapr Sidecar Host
  2. const daprPort = "3500"; // Dapr Sidecar Port of this Example Server
  3. const serverHost = "127.0.0.1"; // App Host of this Example Server
  4. const serverPort = "50051"; // App Port of this Example Server
  5. // HTTP
  6. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
  7. const client = new DaprClient(daprHost, daprPort);
  8. // GRPC
  9. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  10. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
DaprClient Library
DaprServer Library

A library for how an application registers bindings / routes with Dapr. The startServer() method is used to start the server and bind the routes.

  1. import { DaprClient, HttpMethod, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  6. const serviceAppId = "my-app-id";
  7. // POST Request
  8. const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.POST, { hello: "world" });
  9. // GET Request
  10. const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
  11. }
  • For a full list of state operations visit .
Publish messages
  1. const daprHost = "127.0.0.1";
  2. const daprPort = "3500";
  3. async function start() {
  4. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  5. const pubSubName = "my-pubsub-name";
  6. const topic = "topic-a";
  7. const message = { hello: "world" }
  8. // Publish Message to Topic
  9. const response = await client.pubsub.publish(pubSubName, topic, message);
  10. }
Subscribe to messages
  1. import { DaprServer, CommunicationProtocolEnum } from "dapr-client";
  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. async function start() {
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  8. const pubSubName = "my-pubsub-name";
  9. const topic = "topic-a";
  10. // Configure Subscriber for a Topic
  11. await server.pubsub.subscribe(pubSubName, topic, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
  12. await server.startServer();

Output Bindings

  1. import { DaprServer, CommunicationProtocolEnum } from "dapr-client";;
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. const serverHost = "127.0.0.1";
  5. const serverPort = "5051";
  6. async function start() {
  7. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);;
  8. const bindingName = "my-binding-name";
  9. const response = await server.binding.receive(bindingName, async (data: any) => console.log(`Got Data: ${JSON.stringify(data)}`));
  10. await server.startServer();
  11. }
  • For a full guide on output bindings visit .
  1. import { DaprClient, CommunicationProtocolEnum } from "dapr-client";
  2. const daprHost = "127.0.0.1";
  3. const daprPort = "3500";
  4. async function start() {
  5. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);
  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. }