HomeGuidesRecipesAPI EndpointsRelease NotesCommunity
Log In

Making API Calls

📘

General Note

This guide follows along with the Layar API Software Developers Kit (SDK). We recommend you have the Github reference documentation open in a separate tab while working through this guide.

The Layar API comes with many different API classes, each serving its own purpose. To learn more, you can view the API Endpoint Overview documentation.

Each API class has its own collection of available methods which can be used in combination to achieve a wide variety of goals and solutions.

In this section, we will provide a brief overview of how to use these methods. More detailed descriptions of what each method does can be found in later sections.

Instantiating API Classes

You will need to instantiate each API class you intend to use. The full list of API classes and subsequent endpoints are available in the API Endpoints Reference.

Instantiating an API class requires calling the configuration you set up in previous sections of the Getting Started Guide (see Configure Your Instance). If you’ve been following along with this guide you have actually already done this!

To instantiate an API class, you use the following boilerplate command, where apiClass is the class you wish to call (e.g. sourceDocument).

# Instantiate API Class
api_instance = layar_api.apiClass(layar_api.ApiClient(configuration))

If you used our Pro Tip from the Configuration section, you can actually make this much cleaner!

# This line is the Pro Tip - to be added underneath the configuration object
client =   layar_api.ApiClient(configuration)

# Instantiate API Class
api_instance = layar_api.apiClass(client)

We'll use this cleaner option for the remainder of the tutorial.

Example 1

Let's instantiate the SourceDocumentApi class.

api_instance = layar_api.SourceDocumentApi(client)

Let’s rename that api_instance variable something more descriptive, like this:

sourceDocumentExample = layar_api.SourceDocumentApi(client)

Example 2

Now, let's add in the QuestionApi below the sourceDocumentExample.

Add the following line:

questionExample = layar_api.QuestionApi(client)

You can continue to add any API classes you think you’ll use by instantiating it with this configuration.

Calling API Methods

Each API class has an assortment of methods associated with it that you will use to read, write, update, and delete data. If you were following along with the Getting Started Guide, you’ve already used the SourceDocumentApi ‘s search() method when you were testing during setup! Let’s look a little closer at how we can get the data we are looking for out of it.

Methods accept arguments that will tell the API what kinds of data you are looking for. The search() method on the SourceDocumentApi accepts two arguments, a body, and x_vyasa_data_providers.

The body argument accepts a SourceDocumentSearchCommand which then accepts arguments of its own that describe the search to the API.

The x_vyasa_data_providers accepts a string value that tells the API which data providers to look in for the data you are searching for. We created a string of data providers back when we were configuring our instance, but we’ll revisit it here as well.

First, let’s set up our body. You should have made one earlier that looked like this:

body = layar_api.SourceDocumentSearchCommand()

Now let’s add some arguments that tell our API what information we are looking for. The full list of arguments you can add is available in the SourceDocumentSearchCommand() documentation. For now, we will try asking for a specific document.

We can ask for a document by its ID by adding the ids argument to our SourceDocumentSearchCommand(). The ids argument accepts a list of strings that are encapsulated in square brackets. Search for the document with the id 2204.13696 by adding it to your ids argument like so:

body = layar_api.SourceDocumentSearchComand(ids=['2204.13696'])

Now we will tell our API where to look for this document. This document lives in the master-arxiv.vyasa.com data provider which is a part of our canonical data fabric included with your Layar instance.

You should provide the data providers you wish to query as a single-string, comma-delimited list. Make sure your x_vyasa_data_providers list includes master-arxiv.vyasa.com and looks something like this:

x_vyasa_data_providers = 'master-arxiv.vyasa.com,master-clinicaltrials.vyasa.com'

We are ready to run our API call! Place the following code at the end of your file:

try:
api_response = sourceDocumentExample.search(body,x_vyasa_data_providers)
pprint(api_response)
except ApiException as e:
print("Exception when calling SourceDocumentApi->search: %s\n" % e)

Now when you run your code, it should print out your result in the console which should look something like this:

[{'annotations': None,
 'append_to_existing': False,
 'column_definitions': [],
 'cortex_document_type': 'DOCUMENT',
 'created_by_user': -1,
 'date_indexed': datetime.datetime(2022, 5, 13, 23, 7, 14, 848000, tzinfo=tzutc()),
 'date_published': datetime.datetime(2022, 4, 28, 0, 0, tzinfo=tzutc()),
 'document_uri': 'https://arxiv.org/abs/2204.13696',
 'entity_count': 0,
 'external_ids': {},
 'file': None,
 'has_vector': False,
 'highlighted_text': None,
 'id': '2204.13696',
 'live_source': None,
 'metadata': {},
 'mime_type': None,
 'name': 'NeurMiPs: Neural Mixture of Planar Experts for View Synthesis',
 'object_key': None,
 'parent_source_document_id': None,
 'project_computation_id': None,
 'projects': [],
 'provider': 'master-arxiv.vyasa.com',
 'raw_text': None,
 'statement_count': 0,
 'suggested_category_id': None,
 'suggested_category_ids': [],
 'suggested_category_rankings': {},
 'summary': None,
 'tabular_based_on_file_name': None,
 'trial_data': {},
 'url': None}]