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.
Last updated
Was this helpful?
🎯 Direct API Call
In this tutorial, you will learn how to start integrating 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 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 for GraphQL API library implementation.
Table Of Contents
Step 0: Pre-requisites
Git
Node v.16+
Step 1: Install 3rd Party Library
npm
npm install graphql-request graphql
yarn
yarn add graphql-request graphql
pnpm
pnpm install graphql-request graphql
npm
npm install node-fetch
yarn
yarn add node-fetch
pnpm
pnpm install node-fetch
Step 2: Set Environment Variable
Create a new .env file:
touch .env
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
yarn add dotenv
pnpm install dotenv
and import the package to be able to inject the environment variable to your application:
index.ts
import { config } from "dotenv";
config();
index.js
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.
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();
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();
index.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();
index.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();
The data variable will return and logged into your terminal as follows: