Configure Your Instance
Now that the Layar API is installed and imported into your project you’ll need to configure your instance.
Authorization
You will need a client ID and secret in order to run any API calls. If you already have these keys, you can skip to the Authentication section.
Getting Your API Keys
In order to access your Layar instance, you’ll first need to set up your API tokens. On your Layar account page click “Generate New API Access Keys”
Layar will generate two keys for you: your Client ID and your Client Secret. KEEP THESE SAFE! Your Client Secret will only be shown once when you create it and is non-recoverable. Keep in mind anyone with access to your API keys will have access to your Layar instance.
Getting Your Authentication Token
Underneath your imported dependencies block, enter the following block of code. Use your Vyasa instance URL (i.e. demo.vyasa.com
) as the envURL
and your API keys for the clientID
and clientSecret
.
##Gather relevant URLS
envUrl = 'https://sandbox.certara.ai'
authUrl = f"{envUrl}/connect/oauth/token"
##ClientID and SecretKey (replace with your own keys)
clientId = 'clientId'
clientSecret = 'clientSecret'
##instantiate token variable
token = requests.post(
authUrl,
data={"grant_type" : "client_credentials"},
auth=(clientId, clientSecret),
headers = {'Content-Type' : 'application/x-www-form-urlencoded'},
).json()["access_token"]
Pro Tip
Future calls with reference the token by its variable name. This will cut down on keystrokes to authenticate when making a request.
Protect Your Keys
Do not store your API Keys anywhere that can be accessed publicly. If this file will be hosted or stored in Github or another publicly available repository use an
env
file to hide your API Keys.
Request Module Needed Parameters
Request is a module available in Python 3.x which allows users to make HTTPS calls via Python code. This gives us the ability to interact directly with the Layar API.
Common Request calls are as follows:
- GET
- POST
- PUT
- DELETE
Our authentication request used `request.post
this POST method requires a URI and Header. In most cases you will also be forwarding a Data parameter as well. For our first API call we will be using POST to search for source documents. Before we do so, we will need to define the url
and header
variable.
Grabbing the Correct URL
The Request URL will be different depending on what sort of action you want to do with the URL. Layar makes it easy with Swagger, to browse all the possible URLs. You can go to the following site, filling in YOUR_LAYAR_ENVIORNMENT
with your environment, https://YOUR_LAYAR_ENVIRONMENT/layar/swagger-ui.html
.
Document Search
For our first API call we will be using the following URL to search for source documents.
https://YOUR_LAYAR_ENVIRONMENT/layar/sourceDocument/search
We will make a variable to easily reference the URL for the request. Making use of envUrl
variable to make it easier.
docSearchUrl = f'{envUrl}/layar/sourceDocument/search'
Defining the Request Header
Now that we have the URL we will need to define the Header. We will use the token
variable that was made above to authenticate our request.
header = {'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f"Bearer {token}",
'X-Vyasa-Client': 'layar',
'X-Vyasa-Data-Providers' : 'sandbox.certara.ai',
'X-Vyasa-Data-Fabric': '22'}
Pro Tip
Notice the
'X-Vyasa-Client': 'layar'
part of the header, this indicates we are requesting access to Layar, this may change depending on what we want to do with the API.
Defining the Request Data
The API requires data forwarded to be JSON. Data returned will also be in JSON. Since we are coding the request in Python, this requires the use of dictionaries. We call this variable body
this variable will change over the course of these guides but will almost always be necessary when making a request.
body = {
'rows': '25',
'start': '0',
'sort': 'dateIndexed',
'sortOrder': 'desc',
'sourceFields': [
'name',
'dateIndexed',
'user',
'liveSource',
'cortexDocumentType',
'columnDefinitions',
'createdByUser',
'thumbnailAvailable',
],
}
How do we know what values to use in the data variable?
All of the API URLs are listed inside your environment at
https://YOUR_LAYAR_ENVIRONMENT/layar/swagger-ui.html
Putting it all Together
Now that we have the URL, Header, and Body. Since we are forwarding JSON data we will be using the JSON parameter when calling requests.post
.
Response = Requests.post(docSearchUrl,
headers = header,
json = body)
##Print the json data to text
print(response.text)
Using your IDE of choice, you can now run code block and it will output all documents in your environment in raw text. The response
variable can be manipulated further if desired.
Updated 6 months ago