• Register

Examples

Grab a key and use some of these helpful examples to see how to get started.

Python

Steven Citron-Pousty contributed this example of retrieving all the keys belonging to a specific user. Remember to edit the example and put in your own API Key, Shared Secret, and a valid username in the query example.

 

import json
from json import JSONEncoder
import time
import hashlib
import http.client

__author__="scitronpousty"
__date__ ="$Jan 11, 2011 10:18:04 AM$"


apiKey = "YourAPIKey"
sharedSecret = "YourSharedSecret"

#Not using because we are only doing read only operations
#sandboxAPIKey = 
#sandboxSharedSecret = 

endpoint = "api.mashery.com"
#sandboxEndpoint = "api.sandbox.mashery.com"

path = "/v2/json-rpc/789"
#sandboxPath = 

def buildAuthParams():
    """This function takes our API key and shared secret and uses it to create the signature that mashery wants """
    authHash = hashlib.md5();
    #time.time() gets the current time since the epoch (1970) with decimals seconds
    temp = str.encode(apiKey + sharedSecret + repr(int(time.time())))
    authHash.update(temp)
    return authHash.hexdigest()



if __name__ == "__main__":

    #make an http connection object pointed at the endpoint
    conn = http.client.HTTPConnection(endpoint)


    #This the mashery JSON-RPC query code
    params = JSONEncoder().encode({'method': "object.query", 'params': ["SELECT name, keys.apikey from applications where username = 'theusernameofyouruser'"], 'id':1})

    #Headers required by mashery
    headers = {"Content-type": "application/json", "Accept": "text/plain", "Content-length": repr(len(params))}

    #send the whole shebang over to mashery 
    conn.request("POST", path + "?apikey=" + apiKey + "&sig=" + buildAuthParams() , params, headers)

    resp = conn.getresponse()

    #response data is a binary string and I want to read it easily
    responseObject = resp.read().decode()

    #doing a little pretty formating to get it to look nice on my output terminal
    print(json.dumps(json.loads(responseObject), sort_keys=True, indent=4))

    #just being safe here
    conn.close()

Docs Navigation