Client

    Pre-requisites

    1. Install the SDK with :
    1. Import the libraries:
    1. import { DaprClient, DaprServer, HttpMethod, 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. // HTTP Example
    7. const client = new DaprClient(daprHost, daprPort);
    8. // GRPC Example
    9. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.GRPC);

    Running

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

    1. import { DaprClient } from "dapr-client";
    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. # 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

    Invoke a Service

    1. import { DaprClient, HttpMethod } 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);
    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. // GET Request
    11. const response = await client.invoker.invoke(serviceAppId , serviceMethod , HttpMethod.GET);
    12. start().catch((e) => {
    13. console.error(e);
    14. process.exit(1);
    15. });

    State Management API

    Save, Get and Delete application state

    1. import { DaprClient } 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);
    6. const serviceStoreName = "my-state-store-name";
    7. // Save State
    8. const response = await client.state.save(serviceStoreName, [
    9. key: "first-key-name",
    10. value: "hello"
    11. },
    12. {
    13. key: "second-key-name",
    14. value: "world"
    15. }
    16. ]);
    17. // Get State
    18. const response = await client.state.get(serviceStoreName, "first-key-name");
    19. // Get Bulk State
    20. const response = await client.state.getBulk(serviceStoreName, ["first-key-name", "second-key-name"]);
    21. // State Transactions
    22. await client.state.transaction(serviceStoreName, [
    23. {
    24. operation: "upsert",
    25. request: {
    26. key: "first-key-name",
    27. value: "new-data"
    28. }
    29. },
    30. {
    31. operation: "delete",
    32. request: {
    33. key: "second-key-name"
    34. }
    35. }
    36. ]);
    37. // Delete State
    38. const response = await client.state.delete(serviceStoreName, "first-key-name");
    39. }
    40. start().catch((e) => {
    41. console.error(e);
    42. process.exit(1);
    43. });

    Query State API

    Publish messages

    1. import { DaprClient } 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);
    6. const pubSubName = "my-pubsub-name";
    7. const message = { hello: "world" }
    8. // Publish Message to Topic
    9. const response = await client.pubsub.publish(pubSubName, topic, message);
    10. }
    11. start().catch((e) => {
    12. console.error(e);
    13. });
    Subscribe to messages
    1. import { DaprServer } 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);
    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.start();
    13. }

    Bindings API

    Invoke Output Binding

    Output Bindings

    1. import { DaprClient } 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);
    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. });

    Retrieve secrets

    Configuration API

    Get Configuration Keys

    1. import { DaprClient } from "dapr-client";
    2. const daprHost = "127.0.0.1";
    3. const daprAppId = "example-config";
    4. async function start() {
    5. const client = new DaprClient(
    6. daprHost,
    7. process.env.DAPR_HTTP_PORT
    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. });