Knack is a powerful low-code platform that allows you to build online databases and custom web applications.
When integrating Knack with your own systems or custom front ends, you can use its REST API to access and manipulate your data programmatically.
This guide introduces how to retrieve and list records from a Knack app using PHP. You’ll learn how to authenticate requests, fetch records from a specific object, and display them in a basic list.
Prerequisites
Before you begin, make sure you have:
- A Knack API key
- Your App ID (found in your Knack dashboard)
- The Object ID or slug of the object you want to retrieve records from
- PHP installed and a basic server environment (e.g., Apache, Nginx)
API Endpoint for Retrieving Records
To retrieve records from an object, you'll use the following endpoint:
GET https://api.knack.com/v1/objects/object_{object_id}/records
You’ll need to pass authentication headers in every request:
X-Knack-Application-Id
: your App IDX-Knack-REST-API-Key
: your API key
Example in PHP
Here’s a simple PHP script that retrieves and lists records from a Knack object:
<?php
$apiKey = 'YOUR_API_KEY';
$appId = 'YOUR_APP_ID';
$objectId = 'YOUR_OBJECT_ID';
$url = "https://api.knack.com/v1/objects/object_$objectId/records";
$headers = [
"X-Knack-Application-Id: $appId",
"X-Knack-REST-API-Key: $apiKey",
"Content-Type: application/json"
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (!empty($data['records'])) {
echo "<ul>";
foreach ($data['records'] as $record) {
echo "<li>" . htmlspecialchars($record['field_1']) . "</li>"; // replace 'field_1' with your actual field key
}
echo "</ul>";
} else {
echo "No records found.";
}
?>
Notes:
- Replace
YOUR_API_KEY
,YOUR_APP_ID
, andYOUR_OBJECT_ID
with your actual values. - Adjust
field_1
to match the field key of the data you want to display (these keys can be found in your Knack app’s developer console or by inspecting the API response).