Skip to content

Latest commit

 

History

History
409 lines (316 loc) · 10.1 KB

direct-api-call.md

File metadata and controls

409 lines (316 loc) · 10.1 KB
description layout
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.
title description tableOfContents outline pagination
visible
true
visible
true
visible
true
visible
visible
true

🎯 Direct API Call

🎯 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:

InputFieldsRequiredValue
AuthorizationheadertrueAirstack API Key
querybodytrueAirstack Queries
variablesbodyfalseVariables 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:

InputFieldsRequiredValue
Content-Typeheadertrue"application/json"
AuthorizationheadertrueAirstack API Key
querybodytrueAirstack Queries
variablesbodyfalseVariables 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:

{% tabs %} {% tab title="graphql-request" %} npm

npm install graphql-request graphql

yarn

yarn add graphql-request graphql

pnpm

pnpm install graphql-request graphql

{% endtab %}

{% tab title="node-fetch" %} npm

npm install node-fetch

yarn

yarn add node-fetch

pnpm

pnpm install node-fetch

{% 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="TypeScript" %} {% code title="index.ts" %}

import { config } from "dotenv";

config();

{% endcode %} {% endtab %}

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

import { config } from "dotenv";

config();

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

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:

{% tabs %} {% tab title="graphql-request (TS)" %}

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();

{% endtab %}

{% tab title="graphql-request (JS)" %}

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

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(query);
    console.log(data);
  } catch (e) {
    throw new Error(e?.message)
  }
}

main();

{% endtab %}

{% tab title="node-fetch (TS)" %}

import fetch from "node-fetch";

interface QueryResponse {
  data: Data;
}

interface Data {
  Wallet: Wallet;
}

interface Error {
  message: string;
}

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

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

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

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

const main = async () => {
  try {
    if (!AIRSTACK_API_KEY) throw new Error("AIRSTACK_API_KEY not set");

    const res = await fetch(AIRSTACK_API_URL, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: AIRSTACK_API_KEY, // Add API key to Authorization header
      },
      body: JSON.stringify({ query }),
    });
    const json = (await res?.json()) as QueryResponse;
    const data = json?.data;

    console.log(data);
  } catch (e) {
    throw new Error((e as Error)?.message);
  }
};

main();

{% endtab %}

{% tab title="node-fetch (JS)" %}

import fetch from "node-fetch";

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

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

const main = async () => {
  try {
    if (!AIRSTACK_API_KEY) throw new Error("AIRSTACK_API_KEY not set");
    
    const res = await fetch(
      AIRSTACK_API_URL,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: AIRSTACK_API_KEY // Add API key to Authorization header
        },
        body: JSON.stringify({ query }),
      }
    );
    const json = await res?.json();
    const data = json?.data;
  
    console.log(data);
  } catch (e) {
    throw new Error(e?.message);
  }
}

main();

{% 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 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: