Skip to content

Instantly share code, notes, and snippets.

@ianozsvald
Created January 5, 2013 23:12
Show Gist options
  • Save ianozsvald/4464247 to your computer and use it in GitHub Desktop.
Save ianozsvald/4464247 to your computer and use it in GitHub Desktop.
Call the AlchemyAPI's GetAPIKeyInfo end point to ask how many API calls we have left today
"""Query AlchemyAPI to determine number of API calls still available"""
# -*- coding: utf-8 -*-
import json
import requests
def get_api_key():
# Load API key (40 HEX character key) from local file
key = open('api_key.txt').readline().strip()
return key
def alchemy_calls_left(api_key):
# Typical response from Alchemy:
#{
# "status": "OK",
# "consumedDailyTransactions": "1020",
# "dailyTransactionLimit": "1000"
#}
# This URL tells us how many calls we have left in a day
URL = "http://access.alchemyapi.com/calls/info/GetAPIKeyInfo?apikey={}&outputMode=json".format(api_key)
# call AlchemyAPI, ask for JSON response
response = requests.get(URL)
calls_left = json.loads(response.content)
return calls_left
def get_calls_left(api_key):
"""Call AlchemyAPI, report still_calls_left and details"""
calls_left = alchemy_calls_left(api_key)
# convert the text number fields to integers
calls_left['consumedDailyTransactions'] = int(calls_left['consumedDailyTransactions'])
calls_left['dailyTransactionLimit'] = int(calls_left['dailyTransactionLimit'])
# add a convenience boolean
calls_left['still_calls_left'] = calls_left['consumedDailyTransactions'] < calls_left['dailyTransactionLimit']
return calls_left
if __name__ == "__main__":
api_key = get_api_key()
print "Using key:", api_key
print "Calls left:", get_calls_left(api_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment