Skip to content

Node.js SDK Guide

Welcome to the Node.js SDK Guide for Mira. This SDK enables Node.js developers to easily interact with Mira's API services, or "flows", directly from their applications. This guide will walk you through the setup, basic usage, and some advanced features of the Node SDK.

Installation

To use the Mira Node.js SDK in your project, you first need to install it via npm or yarn:

bash
npm install @mira-network/sdk

or

bash
yarn add @mira-network/sdk

Configuration

Before you begin, ensure you have your API keys ready. You will need these to authenticate your requests. Here’s how to configure the SDK with your API keys:

javascript
import { Client } from "@mira-network/sdk";

const client = new Client({
  apiKey: "YOUR_API_KEY",
});

Making Your First Request

Now that you have the SDK installed and configured, you can make your first request. Here’s how to use the Client to execute a flow:

javascript
import { Client } from "@mira-network/sdk";

const client = new Client({
  apiKey: "YOUR_API_KEY",
});

async function executeFlow() {
  const input = {
    query: "What is the capital of France?",
  };

  try {
    const output = await client.run("@klok/qna", input);
    console.log("Output:", output);
  } catch (error) {
    console.error("Error executing flow:", error);
  }
}

executeFlow();

Handling Responses and Errors

It’s important to handle responses and potential errors properly. The SDK throws exceptions for errors, so using try-catch blocks is essential for robust error handling:

javascript
async function executeFlow() {
  const input = {
    query: "What is the tallest building in the world?",
  };

  try {
    const output = await client.run("@klok/building-height", input);
    console.log("Output:", output);
  } catch (error) {
    console.error("Error:", error);
  }
}

Advanced Usage

Asynchronous Calls

Node.js is designed to handle asynchronous operations. The Mira SDK takes full advantage of this with its asynchronous methods, allowing non-blocking calls to flows:

javascript
// Example of using async/await with the Mira SDK
async function fetchData() {
  const data = await client.run("@klok/weather", { city: "New York" });
  console.log(data);
}

Caching

For performance optimization, especially when dealing with frequent API calls to the same flow, implementing caching mechanisms may be beneficial:

javascript
// Example pseudo-code for caching a flow response
const cache = {};

async function getCachedData(flowName, params) {
  const key = `${flowName}-${JSON.stringify(params)}`;
  if (cache[key]) {
    return cache[key]; // Return cached response if available
  }

  const response = await client.run(flowName, params);
  cache[key] = response; // Store response in cache
  return response;
}

Conclusion

The Mira Node.js SDK is a powerful tool for integrating Mira's API services into your Node.js applications. By following this guide, you should be able to set up the SDK, make your first requests, and implement advanced features to ensure your applications run smoothly and efficiently. For further assistance or more complex integration scenarios, refer to our Community Support or check the API documentation.