đŸŽ¯Direct API Call

Learn how to use Airstack by making direct API call without Airstack SDK. In this tutorial, you will learn how to integrate in Node.js. However, this should also work in any other tech stacks.

đŸŽ¯ Direct API Call

In this tutorial, you will learn how to start integrating Airstack API by making direct API call without the Airstack SDKs.

While this method is most useful when you already have an app that is using languages not supported by the Airstack SDKs, such as JavaScript/TypeScript and Python, this get starting guide will be using Node.js as an example.

All the concepts here for integrating Airstack GraphQL API should be translatable to any tech stacks you are using.

GraphQL API Library

You can call the Airstack API by using a 3rd party GraphQL API library with the following details:

Input
Fields
Required
Value

Authorization

header

query

body

Airstack Queries

variables

body

Variables to the Queries

In this tutorial, you'll be shown example with graphql-request for GraphQL API library implementation.

REST API Library

You can also call the Airstack API by using a traditional 3rd party REST API library, such as node-fetch, with the following details:

Input
Fields
Required
Value

Content-Type

header

"application/json"

Authorization

header

query

body

Airstack Queries

variables

body

Variables to the Queries

In this tutorial, you'll be shown example with node-fetch for GraphQL API library implementation.

Table Of Contents

Step 0: Pre-requisites

Step 1: Install 3rd Party Library

Use a package manager to install the 3rd party library into your Node.js project:

npm

npm install graphql-request graphql

yarn

yarn add graphql-request graphql

pnpm

pnpm install graphql-request graphql

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:

npm install dotenv

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

index.ts
import { config } from "dotenv";

config();

Step 3: Call Your Query

Once you have installed one of the 3rd party SDK, you can directly make calls to the Airstack API.

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

import { gql, GraphQLClient } from "graphql-request";

interface Data {
  Wallet: Wallet;
}

interface Error {
  message: string;
}

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

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

const AIRSTACK_API_URL = "https://api.airstack.xyz/graphql";
const AIRSTACK_API_KEY = process.env.AIRSTACK_API_KEY;

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

if (!AIRSTACK_API_KEY) throw new Error("AIRSTACK_API_KEY not set");

const graphQLClient = new GraphQLClient(AIRSTACK_API_URL, {
  headers: {
    Authorization: AIRSTACK_API_KEY, // Add API key to Authorization header
  },
});

const main = async () => {
  try {
    const data = await graphQLClient.request<Data>(query);
    console.log(data);
  } catch (e) {
    throw new Error((e as Error)?.message)
  }
}

main();

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 using direct API call into your web3 application, please join our Airstack's Telegram group.

More Resources

Learn to build more with Airstack using our tutorials:

Last updated

Was this helpful?