Skip to content

Latest commit

 

History

History
277 lines (216 loc) · 5.62 KB

node.md

File metadata and controls

277 lines (216 loc) · 5.62 KB
description layout
Learn how to integrate Airstack APIs to your Node.js application using the Airstack Node SDK.
title description tableOfContents outline pagination
visible
true
visible
true
visible
true
visible
visible
true

🗼 Node.js

🗼 Node.js

In this tutorial, you will learn how to start integrating Airstack API into your Node.js application.

Table Of Contents

Step 0: Pre-requisites

Step 1: Install Airstack Node SDK

Use a package manager to install the Airstack Node SDK into your Node.js project:

{% tabs %} {% tab title="npm" %}

npm install @airstack/node

{% endtab %}

{% tab title="yarn" %}

yarn add @airstack/node

{% endtab %}

{% tab title="pnpm" %}

pnpm install @airstack/node

{% endtab %} {% endtabs %}

Step 2: Set Environment Variable

Create a new .env file:

touch .env

Add the Airstack API key as the environment variable:

AIRSTACK_API_KEY=YOUR_AIRSTACK_API_KEY

If you are using Node version 20.6.0+, then you can simply import the environment variable to you Node.js app. Thus, directly proceed to the next step.

If you are using Node version earlier than 20.6.0, then you need to install the dotenv package:

{% tabs %} {% tab title="npm" %}

npm install dotenv

{% endtab %}

{% tab title="yarn" %}

yarn add dotenv

{% endtab %}

{% tab title="pnpm" %}

pnpm install dotenv

{% endtab %} {% endtabs %}

and import the package to be able to inject the environment variable to your application:

{% tabs %} {% tab title="JavaScript" %} {% code title="index.js" %}

import { config } from "dotenv";

config();

{% endcode %} {% endtab %}

{% tab title="TypeScript" %} {% code title="index.ts" %}

import { config } from "dotenv";

config();

{% endcode %} {% endtab %} {% endtabs %}

Step 3: Initialize SDK

You can use init from the SDK to initialize it with the Airstack API key:

{% tabs %} {% tab title="TypeScript" %} {% code title="index.ts" %}

import { init } from "@airstack/node";

init(process.env.AIRSTACK_API_KEY);

{% endcode %} {% endtab %}

{% tab title="JavaScript" %} {% code title="index.js" %}

import { init } from "@airstack/node";

init(process.env.AIRSTACK_API_KEY);

{% endcode %} {% endtab %} {% endtabs %}

Step 4: Call Your Query

Once you have initialized the SDK, you can use the fetchQuery to call the Airstack API.

Below you have been provided with Airstack query to fetch the 0x address, Lens, and Farcaster owned by vitalik.eth:

{% hint style="info" %} For more query examples, check out Guides for various use cases you can build with Airstack. {% endhint %}

{% tabs %} {% tab title="TypeScript" %} {% code title="index.ts" %}

import { fetchQuery } from "@airstack/node";

interface QueryResponse {
  data: Data;
  error: Error;
}

interface Data {
  Wallet: Wallet;
}

interface Error {
  message: string;
}

interface Wallet {
  socials: Social[];
  addresses: string[];
}

interface Social {
  dappName: "farcaster";
  profileName: string;
}

const query = `
query MyQuery {
  Wallet(input: {identity: "vitalik.eth", blockchain: ethereum}) {
    socials {
      dappName
      profileName
    }
    addresses
  }
}
`;

const main = async () => {
  const { data, error }: QueryResponse = await fetchQuery(query);

  if (error) {
    throw new Error(error.message);
  }

  console.log(data);
};

main();

{% endcode %} {% endtab %}

{% tab title="JavaScript" %} {% code title="index.js" %}

import { fetchQuery } from "@airstack/node";

const query = `
query MyQuery {
  Wallet(input: {identity: "vitalik.eth", blockchain: ethereum}) {
    socials {
      dappName
      profileName
    }
    addresses
  }
}
`;

const main = async () => {
  const { data, error } = await fetchQuery(query);

  if (error) {
    throw new Error(error.message);
  }

  console.log(data);
};

main();

{% endcode %} {% endtab %} {% endtabs %}

The data variable will return and logged into your terminal as follows:

{
  "data": {
    "Wallet": {
      "socials": [
        {
          "dappName": "farcaster",
          "profileName": "vitalik.eth"
        }
      ],
      "addresses": ["0xd8da6bf26964af9d7eed9e03e53415d37aa96045"]
    }
  }
}

Developer Support

If you have any questions or need help regarding integrating Airstack into your Node.js application, please join our Airstack's Telegram group.

More Resources

Learn to build more with Airstack using our tutorials: