Nansen Query Documentation
Search…
⌃K
⚙

Sample Python Code

Sample code for accessing the Nansen API, retrieves the relevant data, puts it in a dataframe and then saves a CSV to the user's local file.
The user just needs to input EMAIL, PASSWORD (hidden), API QUESTION, and TOKEN_ADDRESS.
Available as either a downloadable file or directly as code.
​
File Download
API_Sample_Code.ipynb
15KB
Binary
Sample Code for Jupyter Notebook
Sample Code
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Importing python librairies\n",
"\n",
"import json\n",
"import requests\n",
"import pandas\n",
"import os\n",
"from getpass import getpass"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input Nansen your email and password please\n",
"What is your Nansen account email? [email protected]\n",
"Please input your Nansen password ········\n"
]
}
],
"source": [
"# User prompted to input his email and password below:\n",
"\n",
"print('Input Nansen your email and password please')\n",
"try:\n",
" EMAIL = input('What is your Nansen account email? ')\n",
"except:\n",
" pass\n",
"try:\n",
" PASSWORD = getpass(prompt='Please input your Nansen password ')\n",
"except:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"200, should be 200\n",
"application/json\n"
]
}
],
"source": [
"# Connecting to the API\n",
"\n",
"# User parameters and API url and headers\n",
"\n",
"url = f'https://zarya-backend-mediator-pidzqxgs7a-uc.a.run.app/auth'\n",
"headers = {'Accept': 'application/json','Content-Type': 'application/json'}\n",
"\n",
"payload = json.dumps({\n",
" 'email': EMAIL,\n",
" 'password': PASSWORD,\n",
"})\n",
"\n",
"r = requests.post(url, headers=headers, data=payload)\n",
"\n",
"print(str(r.status_code) + ', should be 200') \n",
"print(r.headers['content-type'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'token': 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjUwYTdhYTlkNzg5MmI1MmE4YzgxMzkwMzIzYzVjMjJlMTkwMzI1ZDgiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vZDUtbmFuc2VuLXByb2QiLCJhdWQiOiJkNS1uYW5zZW4tcHJvZCIsImF1dGhfdGltZSI6MTY1NzA0OTMzNSwidXNlcl9pZCI6Ijg5ZkMwMTRreDJRVE96bm5FYjVrcXRWZ2FJdjIiLCJzdWIiOiI4OWZDMDE0a3gyUVRPem5uRWI1a3F0VmdhSXYyIiwiaWF0IjoxNjU3MDQ5MzM1LCJleHAiOjE2NTcwNTI5MzUsImVtYWlsIjoiYXVyZWxpZS5iYXJ0aGVyZUBuYW5zZW4uYWkiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJlbWFpbCI6WyJhdXJlbGllLmJhcnRoZXJlQG5hbnNlbi5haSJdfSwic2lnbl9pbl9wcm92aWRlciI6InBhc3N3b3JkIn19.BihuSukAMWdwIaVedZLuIPCLOVX_cUeH-9KGzNQbwXWEHgbmNs-Qw0OryoXXOzv1AEIqvRQv5g78SPSlrF-R1AyfhqL4vbytZGn4dts-5Kwz3aRepvqpBO79a7JjDV6iTScnAp_zMMqundIlIflxqz6OEadsT0kxigd0HpzQzmtK03EruUuhRxvESHLgd2BhN5BFpOvD2LUz2p-pnZCflhCoUZ86mivzUINYVEJSPu_Ps9SeGkUA4W47RAgeMRMmlNjl32zwfwdH0OL5XoxFArfBBqJpQ42lM_O0qy6Xqh8wIsD6XXS7B5QCZ5NM3qTj1fbTNVFmvkLrgMy8ZFIyUQ', 'uid': '89fC014kx2QTOznnEb5kqtVgaIv2'}\n"
]
}
],
"source": [
"# Retrieving user token and displaying that token\n",
"\n",
"token = r.json()\n",
"print(token)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Testing a few sample API questions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Examples of questions:\n",
"\n",
"question_1 = f'/v1/questions/api_tgm_balance_change_7d'\n",
"question_2 = f'/v1/questions/api_tgm_token_balances'\n",
"question_3 = f'/v1/questions/api_token_balances_on_exchanges'\n",
"question_4 = f'/v1/questions/api_sm_holdings_segment'\n",
"question_5 = f'/v1/questions/api_tgm_total_exchange_supply'\n",
"\n",
"\n",
"token_address_1 = \"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48\" # usdc example"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please input an API question, example: /v1/questions/api_nft_mint_leaderboard; see https://nansen-ai.gitbook.io/nansen-query-documentation/api-access/end-points/v1/questions/api_nft_mint_leaderboard\n",
"Please input a token address, example USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB480xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48\n"
]
}
],
"source": [
"# User please input your question \n",
"\n",
"try:\n",
" user_input_question = input('Please input an API question, example: /v1/questions/api_nft_mint_leaderboard; see https://nansen-ai.gitbook.io/nansen-query-documentation/api-access/end-points')\n",
"except:\n",
" pass\n",
"try:\n",
" user_input_token = input('Please input a token address, example USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')\n",
"except:\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Loading data to Python dictionary\n",
"url = f'https://zarya-backend-mediator-pidzqxgs7a-uc.a.run.app' + user_input_question\n",
"token_address = {\"token\": user_input_token}\n",
"headers = {'Accept': 'application/json','Content-Type': 'application/json','Authorization': token['token']}\n",
"payload = json.dumps({\"params\": token_address,\"accept_stale\": False})\n",
"\n",
"try: \n",
" r = requests.post(url, headers=headers, data=payload)\n",
" r.json()\n",
" \n",
"except:\n",
" print('There was an error attempting to call the API. Please check the correctness of your token and question inputs and try again.')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is what your data look like in a dataframe:\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>address</th>\n",
" <th>no_of_5x_projects</th>\n",
" <th>no_of_nfts_sold</th>\n",
" <th>no_of_projects_sold</th>\n",
" <th>profit</th>\n",
" <th>revenue</th>\n",
" <th>spent</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0xee0201e07db325a5d732aeb9b9fba7b5b8e92c2a</td>\n",
" <td>30</td>\n",
" <td>397</td>\n",
" <td>68</td>\n",
" <td>18.163534</td>\n",
" <td>34.518600</td>\n",
" <td>16.355066</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0x97da20dc2bd46174da79d71a385b7248527fbee6</td>\n",
" <td>15</td>\n",
" <td>709</td>\n",
" <td>36</td>\n",
" <td>17.257880</td>\n",
" <td>32.833920</td>\n",
" <td>15.576040</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0x73c4818b537a4108d3e90dc83baabe80405c66d8</td>\n",
" <td>16</td>\n",
" <td>603</td>\n",
" <td>86</td>\n",
" <td>17.171500</td>\n",
" <td>43.231800</td>\n",
" <td>26.060300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0xd8b07bc1bc3bae553bca5e94e99935dc12df24ff</td>\n",
" <td>14</td>\n",
" <td>161</td>\n",
" <td>51</td>\n",
" <td>15.293985</td>\n",
" <td>27.226202</td>\n",
" <td>11.932217</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0xd045cc3e4428222eeb1602d63897a32c32440a6c</td>\n",
" <td>20</td>\n",
" <td>471</td>\n",
" <td>40</td>\n",
" <td>14.119944</td>\n",
" <td>23.688700</td>\n",
" <td>9.568756</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>95</th>\n",
" <td>0x165d163657ed71b588b2c05c7e477dabfb214c87</td>\n",
" <td>19</td>\n",
" <td>258</td>\n",
" <td>58</td>\n",
" <td>5.073793</td>\n",
" <td>8.686973</td>\n",
" <td>3.613180</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96</th>\n",
" <td>0xf82d46e02dd50c280050d45f22d7e22aceb0f0ae</td>\n",
" <td>22</td>\n",
" <td>104</td>\n",
" <td>48</td>\n",
" <td>5.070906</td>\n",
" <td>10.345000</td>\n",
" <td>5.274094</td>\n",
" </tr>\n",
" <tr>\n",
" <th>97</th>\n",
" <td>0x911ab8b7d4a95a81653cc42122b8f3972dcff898</td>\n",
" <td>38</td>\n",
" <td>192</td>\n",
" <td>67</td>\n",
" <td>5.034235</td>\n",
" <td>6.141130</td>\n",
" <td>1.106895</td>\n",
" </tr>\n",
" <tr>\n",
" <th>98</th>\n",
" <td>0xa184c1285a66bf2082a78962356713a460951822</td>\n",
" <td>16</td>\n",
" <td>326</td>\n",
" <td>52</td>\n",
" <td>5.004661</td>\n",
" <td>7.497680</td>\n",
" <td>2.493019</td>\n",
" </tr>\n",
" <tr>\n",
" <th>99</th>\n",
" <td>0x8524915f14e267c1786d7b89768428553e7674dc</td>\n",
" <td>13</td>\n",
" <td>133</td>\n",
" <td>35</td>\n",
" <td>5.004618</td>\n",
" <td>11.202200</td>\n",
" <td>6.197582</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>100 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" address no_of_5x_projects \\\n",
"0 0xee0201e07db325a5d732aeb9b9fba7b5b8e92c2a 30 \n",
"1 0x97da20dc2bd46174da79d71a385b7248527fbee6 15 \n",
"2 0x73c4818b537a4108d3e90dc83baabe80405c66d8 16 \n",
"3 0xd8b07bc1bc3bae553bca5e94e99935dc12df24ff 14 \n",
"4 0xd045cc3e4428222eeb1602d63897a32c32440a6c 20 \n",
".. ... ... \n",
"95 0x165d163657ed71b588b2c05c7e477dabfb214c87 19 \n",
"96 0xf82d46e02dd50c280050d45f22d7e22aceb0f0ae 22 \n",
"97 0x911ab8b7d4a95a81653cc42122b8f3972dcff898 38 \n",
"98 0xa184c1285a66bf2082a78962356713a460951822 16 \n",
"99 0x8524915f14e267c1786d7b89768428553e7674dc 13 \n",
"\n",
" no_of_nfts_sold no_of_projects_sold profit revenue spent \n",
"0 397 68 18.163534 34.518600 16.355066 \n",
"1 709 36 17.257880 32.833920 15.576040 \n",
"2 603 86 17.171500 43.231800 26.060300 \n",
"3 161 51 15.293985 27.226202 11.932217 \n",
"4 471 40 14.119944 23.688700 9.568756 \n",
".. ... ... ... ... ... \n",
"95 258 58 5.073793 8.686973 3.613180 \n",
"96 104 48 5.070906 10.345000 5.274094 \n",
"97 192 67 5.034235 6.141130 1.106895 \n",
"98 326 52 5.004661 7.497680 2.493019 \n",
"99 133 35 5.004618 11.202200 6.197582 \n",
"\n",
"[100 rows x 7 columns]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Converting our data to pandas.DataFrame\n",
"\n",
"try: \n",
" df = pandas.DataFrame.from_dict(r.json()['result_data'])\n",
"except:\n",
" df = pandas.DataFrame.from_dict(r.json())\n",
"\n",
"print('Here is what your data look like in a dataframe:')\n",
"df\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"An example Excel workbook with the dataframe output has been saved here: C:\\Users\\aurel\\example_nansen_api.csv\n"
]
}
],
"source": [
"# Moving to excel INPUT to YOUR LOCAL DRIVE\n",
"\n",
" # Find the path where this workbook is saved\n",
"\n",
"your_local_path = os.path.dirname(os.path.abspath(os.getcwd()))\n",
"\n",
" # Saving to csv\n",
"\n",
"csv_name = 'example_nansen_api.csv'\n",
" \n",
"df.to_csv(your_local_path+ r'\\\\'+csv_name)\n",
"\n",
"print('An example Excel workbook with the dataframe output has been saved here: '+ your_local_path+ '\\\\' + csv_name)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enjoy Nansen API ;-)\n"
]
}
],
"source": [
"print('Enjoy Nansen API ;-)')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
​