By setting up your API key on the Nuklai marketplace, you will be able to query your data through our REST API. You can also integrate the Nuklai REST API into the applications that you are building as a developer or a data professional.
In this user guide, we will show you how to set up your API and query your first dataset.
To use Nuklai’s REST API, you should have:
Knowledge of programming languages and the query language SQL
When you arrive on the Nuklai marketplace, in the sidebar there will be a menu item called Settings. When you click on this menu item, a sub-menu will unfold, and you will see a sub-menu called API key. When you click on this sub-menu, a screen will appear where you can generate your API key.
The API key is set up with the rights to query all datasets that the user has uploaded to the Nuklai marketplace and the user can query all datasets that have an active subscription.
You will also be asked to select how many days the API key stays valid in the Duration Days field. This is done for security purposes – we suggest renewing your API keys every 90 days, but you can select a time between 1 day and 365 days.
Give your API key a name to distinguish them from other active API keys. You will be able to generate up to 5 different keys.
Now click the Generate button. In the API Key field, you will see a long string. This string is your API key. You will need to store this key in a secure place – if you lose this key, you will need to generate a new one. You can click the copy button (two squares) to copy the API key.
Send a query via the API
You can now open up your preferred code editor, if you have not set up any code editor and if you are familiar with Python, you can also choose to set up a Jupyter Notebook. We recommend you follow the official guide for installing JupyterLab on your device.
To set up your API in your code editor you should set up three variables. The API url, the API key, and the DatasetID.
HTTP Method: POST
API Url: https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries
Response format: JSON
The Dataset-ID you can get from the url of the dataset you want to query. It is a 36-character hexadecimal string and it is split into 5 parts.
For example: https://app.nukl.ai/dataset/061e6d78-205e-43e0-b9c1-499cea3b759c
Where 061e6d78-205e-43e0-b9c1-499cea3b759c is the Dataset-ID.
// Setting up the variablesconstApiUrl="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries";constApiKey="[API_KEY]";constDatasetId="[DATASET_ID]";
# Setting up the variablesApiUrl ="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries"ApiKey ="[API_KEY]"DatasetId ="[DATASET_ID]"
// Setting up the variables$ApiUrl ="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries";$ApiKey ="[API_KEY]";$DatasetId ="[DATASET_ID]";
To make a proper query you should also set the headers. The first one is for the content type (JSON) and the second is for the authentication.
// Setting up the headerconstheaders= {"Content-Type":"application/json",'authentication': ApiKey}
# Setting up the headerheaders ={"Content-Type":"application/json","authentication": ApiKey}
// Setting up the header$headers = ["Content-Type: application/json","authentication: $ApiKey"];
You are now able to add an SQL query to the body of the dataset. In this example, we will query all columns and the first 5 records of the dataset by writing the following SQL query:
// Selecting 5 records from the datasetselect*from @dataset limit5
This query is added to the body in the following way:
// @dataset represents your dataset rows as a tableconstbody= { sqlQuery:"select * from @dataset limit 5",}
# @dataset represents your dataset rows as a tablebody ={"sqlQuery":"select * from @dataset limit 5"}
// @dataset represents your dataset rows as a table$body = ["sqlQuery"=>"select * from @dataset limit 5"];
Your code should now look like this to make the query:
// variablesconstApiUrl="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries";constApiKey="[API_KEY]";constDatasetId="[DATASET_ID]";// headerconstheaders= {"Content-Type":"application/json",'authentication': ApiKey}// body with sql queryconstbody= { sqlQuery:"select * from @dataset limit 5",}// make a requestfetch(ApiUrl.replace(':datasetId', DatasetId), { method:"POST", headers: headers, body:JSON.stringify(body),// convert to json object}).then((response) =>response.json()).then((data) => {console.log(data); }).catch((error) => {console.error(error); });
# pip install requests# pip install jsonimport requestsimport json# variablesApiUrl ="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries"ApiKey ="[API_KEY]"DatasetId ="[DATASET_ID]"# header headers ={"Content-Type":"application/json","authentication": ApiKey}# body with sql querybody ={"sqlQuery":"select * from @dataset limit 5"}# make a requesturl = ApiUrl.replace(':datasetId', DatasetId)try: response = requests.post(url, headers=headers, data=json.dumps(body)) data = response.json()print(data)except requests.RequestException as error:print(f"Error: {error}")
// variables$ApiUrl ="https://api.nukl.ai/api/public/v1/datasets/:datasetId/queries";$ApiKey ="[API_KEY]";$DatasetId ="[DATASET_ID]";// header$headers = ["Content-Type: application/json","authentication: $ApiKey"];// body with sql query$body = ["sqlQuery"=>"select * from @dataset limit 5"];// make a request$ch =curl_init(str_replace(':datasetId', $DatasetId, $ApiUrl));curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); // convert to json objectcurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);$result =curl_exec($ch);curl_close($ch);echo $result;