Querying the MEME contract with the Covalent API and Python.

adam.xyz
Covalent
Published in
2 min readMar 16, 2021

For the second piece in my series of utilizing the Covalent API, I will walk through how to query the MEME smart contract for NFT metadata.

We start with importing the necessary libraries into our Jupyter Notebook session:

import requests
import json
import urllib.request
from PIL import Image

Next, we send a get request to the Covalent API endpoint in the following format:

/v1/{chainId}/tokens/{address}/nft_metadata/{tokenId}/

  • chainId: 1 (Id number for Ethereum)
  • address: 0xe4605d46fd0b3f8329d936a8b258d69276cba264 (MEME contract)
  • tokenId: 123 (for the purpose of this example)

With all the correct inputs, our get request will look like this:

result = requests.get("https://api.covalenthq.com/v1/1/tokens/0xe4605d46fd0b3f8329d936a8b258d69276cba264/nft_metadata/123/?&key=YOUR_KEY_HERE")

Next, we format the result into a JSON:

result = result.json()
result

Which outputs:

{'data': {'updated_at': '2021-03-16T19:36:24.285356547Z',
'items': [{'contract_decimals': 0,
'contract_name': 'Meme Ltd.',
'contract_ticker_symbol': 'MEMES',
'contract_address': '0xe4605d46fd0b3f8329d936a8b258d69276cba264',
'supports_erc': ['erc20'],
'logo_url': 'https://logos.covalenthq.com/tokens/0xe4605d46fd0b3f8329d936a8b258d69276cba264.png',
'type': 'nft',
'balance': None,
'quote_rate': None,
'quote': None,
'nft_data': [{'token_id': '123',
'token_balance': '1',
'token_url': 'https://api.dontbuymeme.com/memes/123',
'supports_erc': ['erc20', 'erc1155'],
'token_price_wei': '6450000000000000000',
'token_quote_rate_eth': '6.45',
'external_data': {'name': 'I wish I was understood',
'description': None,
'image': 'https://images.dontbuymeme.com/artist-series/fewocious/static/i-wish-i-was-understood.jpg',
'external_url': 'https://dontbuymeme.com/artist-series/fewocious',
'attributes': [{'trait_type': 'Set',
'value': 'Artist Drop 8 - Fewocious'},
{'trait_type': 'Artist', 'value': 'Fewocious'},
{'trait_type': 'Type', 'value': 'Pop Surreal'},
{'display_type': 'date',
'trait_type': 'birthday',
'value': 1609267020},
{'trait_type': 'Max Supply', 'value': '100'}],
'owner': None},
'owner': None}]}],
'pagination': None},
'error': False,
'error_message': None,
'error_code': None}

From this output, we see that the Covalent API is giving us a HUGE amount of info. Amazing.

Since the information we want is nested, we can filter the JSON down slightly:

result['data']['items']

Which outputs the information we are looking for:

[{'contract_decimals': 0,
'contract_name': 'Meme Ltd.',
'contract_ticker_symbol': 'MEMES',
'contract_address': '0xe4605d46fd0b3f8329d936a8b258d69276cba264',
'supports_erc': ['erc20'],
'logo_url': 'https://logos.covalenthq.com/tokens/0xe4605d46fd0b3f8329d936a8b258d69276cba264.png',
'type': 'nft',
'balance': None,
'quote_rate': None,
'quote': None,
'nft_data': [{'token_id': '123',
'token_balance': '1',
'token_url': 'https://api.dontbuymeme.com/memes/123',
'supports_erc': ['erc20', 'erc1155'],
'token_price_wei': '6450000000000000000',
'token_quote_rate_eth': '6.45',
'external_data': {'name': 'I wish I was understood',
'description': None,
'image': 'https://images.dontbuymeme.com/artist-series/fewocious/static/i-wish-i-was-understood.jpg',
'external_url': 'https://dontbuymeme.com/artist-series/fewocious',
'attributes': [{'trait_type': 'Set',
'value': 'Artist Drop 8 - Fewocious'},
{'trait_type': 'Artist', 'value': 'Fewocious'},
{'trait_type': 'Type', 'value': 'Pop Surreal'},
{'display_type': 'date', 'trait_type': 'birthday', 'value': 1609267020},
{'trait_type': 'Max Supply', 'value': '100'}],
'owner': None},
'owner': None}]}]

From here, there are an endless array of possibilities. If you want to access the image URL from within Jupyter notebook and see what the NFT looks like, copy the URL under ‘image’, and set it to URL:

URL = 'https://images.dontbuymeme.com/artist-series/fewocious/static/i-wish-i-was-understood.jpg'

Next:

with urllib.request.urlopen(URL) as url:
with open('nft.jpg', 'wb') as f:
f.write(url.read())
img = Image.open('nft.jpg')img.show()

Which will display:

Fewocious: I Wish I Was Understood

Till next time.

--

--