> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stateset.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Execute NSR-L code (facts, rules, queries)

> **Required scope:** write

**Required scope:** `write`

### Request body

`NSRLExecuteRequest`

<ParamField body="code" type="string" required>
  NSR-L code to execute
</ParamField>

<ParamField body="persist" type="boolean">
  Whether to persist facts/rules to the org-scoped NSR-L program store (and mirror compatible items into the KB)
</ParamField>

### Response

`NSRLExecuteResponse`

<ResponseField name="errors" type="string[]" required>
  Errors (if any)
</ResponseField>

<ResponseField name="execution_time_ms" type="integer (int64)" required>
  Execution time in milliseconds
</ResponseField>

<ResponseField name="facts_added" type="integer" required>
  Facts added
</ResponseField>

<ResponseField name="query_results" type="QueryResultResponse[]" required>
  Query results (for ?- queries)

  <Expandable title="QueryResultResponse">
    <ResponseField name="bindings" type="object[]" required />

    <ResponseField name="confidence" type="number (double)" required />

    <ResponseField name="proof_steps" type="string[]" />

    <ResponseField name="query" type="string" required />

    <ResponseField name="result" type="boolean" required />
  </Expandable>
</ResponseField>

<ResponseField name="rules_added" type="integer" required>
  Rules added
</ResponseField>

<ResponseField name="success" type="boolean" required>
  Whether execution succeeded
</ResponseField>

### Status codes

| Code  | Meaning                                            |
| ----- | -------------------------------------------------- |
| `200` | Execution result                                   |
| `401` | Unauthorized — missing or invalid credentials      |
| `403` | Forbidden — the key/token lacks the required scope |
| `429` | Rate limited — see Retry-After / X-RateLimit-Reset |

<RequestExample>
  ```bash cURL theme={null}
  curl --request POST \
    --url 'https://api.nsr.stateset.com/api/v1/nsrl/execute' \
    --header 'X-API-Key: YOUR_API_KEY' \
    --header 'Content-Type: application/json' \
    --data '{
    "code": "human(socrates). mortal(X) :- human(X). ?- mortal(socrates).",
    "persist": true
  }'
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "errors": [
      "string"
    ],
    "execution_time_ms": 1,
    "facts_added": 1,
    "query_results": [
      {
        "bindings": [],
        "confidence": 1,
        "proof_steps": [],
        "query": "string",
        "result": true
      }
    ],
    "rules_added": 1,
    "success": true
  }
  ```
</ResponseExample>
