REST API Quick Start Example
7 min
in this tutorial, we're going to walk through some sample code that shows you how to authenticate using the api this example will demonstrate how you can request a token and then submit a request to obtain a list of blueprint projects request a token we'll start by obtaining a token by submitting an rest api authenticate request docid\ xfwh5 pdillywazkmg3z1 request the authentication request must be sent to http //today/authentication/v1/login using the get method the request header must contain an authorization header in the following format authorization basic \<encodedcredentials> where \<encodedcredentials> is the base 64 encoded string for the credentials in the following format \<username> \<password> example if the username is jsmith and the password is pa55w0rd , you must calculate the base 64 encoded string for jsmith\ pa55w0rd the base 64 encoded string is anntaxroolbhntv3mhjk , and therefore the authorization header looks like this authorization basic anntaxroolbhntv3mhjk if your credentials are correct, the response contains the access token that you can use to submit api requests if the authorization header is not formatted correctly or if your credentials are incorrect, the service returns a 401 unauthorized call error you can obtain the token expiry by viewing the header of the authenticate response the token expiry is stored in the blueprinttokenexpirydate parameter of the authenticate response header python example the following code demonstrates how you can obtain an access token using python def get token() \# of course, we wouldn't normally hardcode credentials username = "api docs user" password = "pa55w0rd" \# concatenate the username and password with as required by rfc2617 auth string = username + ' ' + password \# encode in utf 8 and perform base64 encoding auth byte = auth string encode('utf 8') auth encode = base64 b64encode(auth byte) \# decode in utf 8 string for the header auth = auth encode decode('utf 8') request uri = 'https //production blueprintcloud com/authentication/v1/login' request header={ 'authorization' 'basic ' + auth, 'accept' 'application/json' } \# make the request using get response = requests get(request uri, headers=request header) token = response json() return token submit request with a request token in hand, you can now submit a request (example rest api list projects request docid\ a1jupmrihxm4wzbhp7ivp ) to obtain data from blueprint you must include your access token in the authorization header of every request the authorization header must look like this authorization blueprinttoken \<tokenvalue> where \<tokenvalue> is the token obtained from the authentication call example authorization blueprinttoken 3l7b+nmqxlpe/dbptnomyzizpzc\[ truncated ]a6cpdscs+uwmib5fodhitoo0fciwp python example the following code demonstrates how you can obtain a list of blueprint projects using python def list projects() \# obtain a token from our get token() function token = get token() \# build the resource uri resource uri = 'https //production blueprintcloud com/api/v1/projects' \# build the request header request header={ 'authorization' 'blueprinttoken ' + token, 'accept' 'application/json' } \# specify the uri parameters uri parameters list={ 'location' 'true' } \# create the request uri request uri = resource uri + '?' + urllib parse urlencode(uri parameters list) output requesturi(request uri) \# submit the request using http get method response = requests get(request uri, headers=request header) return response parse the result after you receive a response, you will need to parse the xml or json data for example, if you requested a list of projects, you will probably want to store the project data in a python list