cURL API Examples¶
These examples call the KnowledgeLake API with cURL from a bash shell. Use them to test a token, try an action, or script a quick job. jq reads the JSON responses.
At a glance
- Tool Bash with
curlandjq(on Windows, Git Bash or WSL) - Tasks on this page Test a token, get a work item, upsert metadata, run a saved search, create a work item and send it to a workflow, list failed jobs
- Token features used Manage Work Items, Manage Workflows, Run Searches, Query Jobs
- Pattern One helper does every JSON call, checks
Success, and fails withMessageKeyorMessages[].MessageKey
For the call shape, the token header and the response envelope, see the Introduction. For every work item action, see the KWorkItem API reference.
Setup¶
export KL_BASE_URL="https://yourcompany.knowledgelake.io/api"
export KL_API_TOKEN="paste-the-token-here" # better: load it from your secret store
# POST one action with a JSON body. Print the body. Fail on HTTP errors or Success false.
kl() {
local action="$1" body="${2:-{\}}" out status
out=$(curl -sS -w '\n%{http_code}' -X POST "$KL_BASE_URL/$action" \
-H "TempIdentityToken: $KL_API_TOKEN" -H "Content-Type: application/json" -d "$body")
status="${out##*$'\n'}"; out="${out%$'\n'*}"
if [ "$status" != "200" ] || [ "$(echo "$out" | jq -r '.Success')" != "true" ]; then
echo "$action failed: HTTP $status $(echo "$out" | jq -r '[.Messages[]?.MessageKey] + [.MessageKey // empty] | join("; ")')" >&2
return 1
fi
echo "$out"
}
The error text is in MessageKey (or Messages[].MessageKey). There is no Message property.
Test the token¶
HTTP 401 means the token is wrong or lacks the Manage Workflows feature. Any action works as a test; pick one your token has.
Get a work item¶
WORK_ITEM_ID="<work item guid>"
kl V1_KWorkItemAPI/GetWorkItem "{\"WorkItemId\":\"$WORK_ITEM_ID\"}" \
| jq '.Data.Documents[] | {Id, Title: .Metadata.Index.Title}'
Upsert metadata¶
# Work item level (routes read these values)
kl V1_KWorkItemAPI/UpsertWorkItemMetadata "{
\"WorkItemId\": \"$WORK_ITEM_ID\",
\"Metadata\": [ { \"Bucket\": \"Index\", \"Key\": \"NeedsReview\", \"Value\": \"Yes\" } ]
}" > /dev/null
# One document
DOCUMENT_ID="<document guid from GetWorkItem>"
kl V1_KWorkItemAPI/UpsertDocumentMetadata "{
\"WorkItemId\": \"$WORK_ITEM_ID\",
\"DocumentId\": \"$DOCUMENT_ID\",
\"Metadata\": [ { \"Bucket\": \"Index\", \"Key\": \"ApprovalStatus\", \"Value\": \"Approved\" } ]
}" > /dev/null
Every value is a string: "true", "1250.00".
Run a saved search¶
SEARCH=$(kl V1_RepositoryAPI/GetSavedSearches '{}' | jq -c '.Data[] | select(.SavedSearchName == "Invoices by Vendor")')
SEARCH_ID=$(echo "$SEARCH" | jq -r '.SavedSearchId')
CATALOG_ID=$(echo "$SEARCH" | jq -r '.SearchCatalogId')
CONDITION_ID=$(echo "$SEARCH" | jq -r '.SearchConditions[] | select(.ParameterInternalName == "VendorName") | .ConditionId')
kl V1_RepositoryAPI/RunSavedSearch "{
\"SearchId\": \"$SEARCH_ID\", \"SearchCatalogId\": \"$CATALOG_ID\",
\"StartRow\": 0, \"RowLimit\": 200,
\"SearchConditions\": { \"Strings\": { \"$CONDITION_ID\": \"Gateway Fleet Services LLC\" } }
}" | jq '.Data.Results[] | .ResultItemId'
Page with StartRow until a page has fewer rows than RowLimit.
Create a work item and send it to a workflow¶
FILE="./Invoice_20481.pdf"
# 1. Create the work item.
NEW_ID=$(kl V1_KWorkItemAPI/NewWorkItem '{"WorkItemName":"Invoice_20481","Metadata":[{"Bucket":"General","Key":"Source","Value":"curl"}]}' \
| jq -r '.Data.WorkItemId')
# 2. Add the file (multipart/form-data). MetadataParameter is a JSON string.
curl -sS -X POST "$KL_BASE_URL/V1_KWorkItemAPI/AddDocument" \
-H "TempIdentityToken: $KL_API_TOKEN" \
-F "WorkItemId=$NEW_ID" \
-F "File=@$FILE;type=application/pdf" \
-F 'MetadataParameter={"Metadata":[{"Bucket":"Index","Key":"InvoiceNumber","Value":"20481"}]}' \
| jq '{Success, Data, Messages}'
# 3. Start the workflow.
WORKFLOW_ID=$(kl V1_WorkflowAPI/GetAllWorkflows | jq -r '.Data[] | select(.WorkflowName == "Invoice Approval") | .WorkflowId')
kl V1_WorkflowAPI/SendToWorkflow "{\"WorkflowId\":\"$WORKFLOW_ID\",\"WorkItemId\":\"$NEW_ID\"}" > /dev/null && echo "Sent $NEW_ID"
With -F, cURL sets the multipart Content-Type. Do not add a JSON Content-Type header to the upload. Check Success in the upload output before you send the work item on.
List failed jobs¶
kl V1_JobsAPI/QueryForJobRuns '{"JobStatus":"Error","PageSize":200}' \
| jq -r '.Data[] | [.JobDisplayName, .JobID, .LastRuntime] | @tsv'
# Rerun one after you checked it (needs Manage Jobs):
# kl V1_JobsAPI/ResubmitJob '{"JobInstanceID":"<JobID>"}' > /dev/null
For a full watchdog, see Find and resubmit failed jobs.
What can go wrong¶
| What you see | Cause and fix |
|---|---|
| The token shows in your shell history | Load it from a file or a secret manager instead of typing it. |
jq: error ... Cannot iterate over null |
The call failed and Data is null. kl prints the reason to stderr. |
| The upload returns HTTP 422 | You added a JSON Content-Type header to the -F upload. Remove it. |
| Quotes break the body | Build bodies with jq -n when values contain quotes. |