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

# Import Policy

> Upload a policy document and extract custom rules using AI

Upload a policy document (file or plain text) and extract enforceable rules using AI. The endpoint returns immediately with a `processing` status while rule extraction runs in the background. Poll [Get Import Details](/api-reference/custom-policies/get-import-details) to check when extraction is complete.

<Note>
  This endpoint sends the document inline in the request body (base64-encoded when `source` is `file`), which is capped by API Gateway / Lambda
  payload limits (a few MB after encoding). For large policy files, use the presigned upload flow
  instead: [Get Import Presigned URL](/api-reference/custom-policies/import-presigned-url) →
  upload to S3 → [Start Import](/api-reference/custom-policies/start-import).
</Note>

## Request Body

<ParamField body="source" type="string" required>
  Content type indicator: `file` for base64-encoded documents or `text` for plain text.
</ParamField>

<ParamField body="content" type="string" required>
  The policy content. Base64-encoded document bytes when `source` is `file`, or plain text when `source` is `text`.
</ParamField>

<ParamField body="filename" type="string">
  Original filename for reference (optional, used with `file` source).
</ParamField>

## Response

<ResponseField name="import_id" type="string">
  Unique identifier for the import. Use this to poll for status, view details, or activate rules.
</ResponseField>

<ResponseField name="status" type="string">
  Initial status: `processing`. Poll the [Get Import Details](/api-reference/custom-policies/get-import-details) endpoint until status transitions to `pending_review`, `no_rules_found`, or `failed`.
</ResponseField>

<ResponseField name="message" type="string">
  Human-readable message with next steps.
</ResponseField>

<ResponseExample>
  ```json 202 Import Accepted theme={null}
  {
    "import_id": "550e8400-e29b-41d4-a716-446655440000",
    "status": "processing",
    "message": "Import accepted. Poll GET /api/policies/import/{import_id} for results."
  }
  ```

  ```json 400 Bad Request theme={null}
  {
    "error": "Missing required field: source"
  }
  ```

  ```json 422 Unprocessable Entity theme={null}
  {
    "error": "Cannot extract text from document"
  }
  ```
</ResponseExample>

## Status Lifecycle

| Status                | Description                                                                                                        |
| --------------------- | ------------------------------------------------------------------------------------------------------------------ |
| `processing`          | Import accepted, AI extraction in progress                                                                         |
| `pending_review`      | Extraction complete, rules ready for review and activation                                                         |
| `no_rules_found`      | Extraction complete but no compliance rules were identified                                                        |
| `failed`              | Extraction failed (see `error_message` in [Get Import Details](/api-reference/custom-policies/get-import-details)) |
| `activated`           | All extracted rules have been activated                                                                            |
| `partially_activated` | Some rules activated, others still pending                                                                         |

## Example

<CodeGroup>
  ```bash cURL (file) theme={null}
  DOC_BASE64=$(base64 -i compliance-policy.pdf)

  # Submit the import
  curl -X POST "https://{api-url}/api/policies/import" \
    -H "x-api-key: YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{
      \"source\": \"file\",
      \"content\": \"$DOC_BASE64\",
      \"filename\": \"compliance-policy.pdf\"
    }"

  # Poll for results (returns 'processing' until extraction completes)
  curl -X GET "https://{api-url}/api/policies/import/IMPORT_ID" \
    -H "x-api-key: YOUR_API_KEY"
  ```

  ```bash cURL (text) theme={null}
  curl -X POST "https://{api-url}/api/policies/import" \
    -H "x-api-key: YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "source": "text",
      "content": "All marketing materials must include a risk disclosure. Performance claims require a 1-year, 5-year, and since-inception comparison to the benchmark."
    }'
  ```

  ```python Python theme={null}
  import requests
  import base64
  import time

  API_KEY = "YOUR_API_KEY"
  API_BASE = "https://{api-url}"

  # Submit file import
  with open("compliance-policy.pdf", "rb") as f:
      doc_bytes = base64.b64encode(f.read()).decode()

  response = requests.post(
      f"{API_BASE}/api/policies/import",
      headers={
          "x-api-key": API_KEY,
          "Content-Type": "application/json"
      },
      json={
          "source": "file",
          "content": doc_bytes,
          "filename": "compliance-policy.pdf"
      }
  )

  import_id = response.json()["import_id"]
  print(f"Import ID: {import_id} — polling for results...")

  # Poll until extraction completes
  while True:
      details = requests.get(
          f"{API_BASE}/api/policies/import/{import_id}",
          headers={"x-api-key": API_KEY}
      ).json()

      if details["status"] != "processing":
          break
      time.sleep(3)

  print(f"Status: {details['status']}")
  if details.get("rule_count") is not None:
      print(f"Rules extracted: {details['rule_count']}")
  ```

  ```javascript JavaScript theme={null}
  const fs = require('fs');
  const axios = require('axios');

  const API_KEY = 'YOUR_API_KEY';
  const API_BASE = 'https://{api-url}';

  const docBytes = fs.readFileSync('compliance-policy.pdf').toString('base64');

  // Submit import
  const { data } = await axios.post(
    `${API_BASE}/api/policies/import`,
    {
      source: 'file',
      content: docBytes,
      filename: 'compliance-policy.pdf'
    },
    {
      headers: {
        'x-api-key': API_KEY,
        'Content-Type': 'application/json'
      }
    }
  );

  console.log(`Import ID: ${data.import_id} — polling for results...`);

  // Poll until extraction completes
  let details;
  do {
    await new Promise(r => setTimeout(r, 3000));
    const res = await axios.get(
      `${API_BASE}/api/policies/import/${data.import_id}`,
      { headers: { 'x-api-key': API_KEY } }
    );
    details = res.data;
  } while (details.status === 'processing');

  console.log(`Status: ${details.status}`);
  if (details.rule_count != null) {
    console.log(`Rules extracted: ${details.rule_count}`);
  }
  ```
</CodeGroup>
