Unified.to
All articles

How to Build Legal Contract Search and Insights with RAG Using Unified's Storage and KMS APIs

·

Retrieval-Augmented Generation (RAG) is one of the most common patterns for building AI systems grounded in enterprise data.

In legal teams, RAG is often used to:

  • Search contracts across multiple repositories
  • Answer clause-level questions
  • Surface renewal terms and termination language
  • Ground AI responses in actual agreement text

But legal RAG systems fail when they:

  • Index documents without respecting permissions
  • Mix tenant data
  • Ignore document structure
  • Treat legal insight as inference instead of retrieval

Unified.to's File Storage and KMS APIs read contracts from file storage and knowledge bases at request time, with one schema per category.

This guide shows how to build a permission-aware, tenant-isolated, incrementally synchronized RAG pipeline for legal contracts using Unified.to.

RAG (Retrieval-Augmented Generation) is not model training.

It is a pattern:

  1. Retrieve relevant contract content.
  2. Provide that content as context.
  3. Generate a response grounded in the retrieved text.

For legal use cases, RAG must be:

  • Deterministic
  • Permission-aware
  • Metadata-driven
  • Non-inferential

Legal RAG answers questions like:

  • Which contracts include indemnification clauses?
  • Which agreements auto-renew?
  • Which vendor contracts expire next quarter?
  • Where is termination for convenience defined?

It does not determine compliance or legal validity.

Unified.to provides the data retrieval layer for this architecture. You own embeddings, vector storage, and model behavior.

Enterprise contracts typically exist in:

  • File Storage (PDF contracts)
  • Knowledge Management systems (Notion, Confluence contract pages)

Unified.to normalizes both.

StorageFile (File Storage API)

Contracts stored as files are represented as StorageFile.

Relevant fields:

  • id
  • created_at
  • updated_at
  • name
  • parent_id
  • user_id
  • size
  • type (FILE or FOLDER)
  • mime_type
  • permissions[]
  • download_url
  • hash
  • version
  • web_url

Important RAG implications:

  • download_url expires after 1 hour (must re-fetch when reprocessing)
  • permissions[] includes:
    • user_id
    • group_id
    • roles (OWNER, READ, WRITE)
  • Explicit permission metadata is available only for file objects, on supported integrations such as Google Drive, Dropbox and Microsoft OneDrive (not Box or SharePoint)
  • Unified.to doesn't enforce access; your RAG system must

This makes StorageFile ideal for embedding contract PDFs with permission metadata attached.

KmsPage (Knowledge Management API)

Contracts stored as structured pages use KmsPage.

Relevant fields:

  • id
  • created_at
  • updated_at
  • title
  • type (HTML, MARKDOWN, TEXT, OTHER)
  • space_id
  • parent_id
  • is_active
  • user_id
  • download_url
  • metadata[]
  • has_children
  • web_url

Important RAG implications:

  • No explicit permissions[] structure
  • Access must be enforced at the application layer
  • Content must be fetched via download_url

RAG pipelines must treat KMS content differently than file storage content.

Step 1: Connect Contract Data Sources

Authorize integrations with Unified.to's embedded Authorization component.

Each integration produces a connection_id.

In RAG systems, connection_id must be treated as the tenant boundary.

All Unified.to endpoints require connection_id. Unified.to enforces isolation at the connection level.

Your vector database must also filter by connection_id.

Step 2: Subscribe to Contract Updates

RAG systems need current content.

Unified.to supports:

  • Native webhooks (the vendor sends events; Unified.to forwards them)
  • Virtual webhooks (on supported integrations, Unified.to detects changes at an interval you set, as often as every minute on paid plans)

Subscribe to:

  • File created
  • File updated
  • Page created
  • Page updated

Page events aren't on every KMS integration (ClickUp, Freshdesk, Gladly and Intercom send none); poll those on a schedule.

Deleted events arrive natively for Confluence, Notion and Zendesk pages and Google Drive and Box files, and through virtual webhooks for Dropbox, OneDrive and SharePoint files (as of 20 October 2026). Elsewhere, list IDs periodically and remove what's gone; updated_gte finds changed records, not deleted ones.

For legal RAG, webhook-driven indexing is strongly preferred over polling.

Step 3: Retrieve Metadata First

Example (Storage):

const files = await sdk.storage.listStorageFiles({
  connectionId,
  limit: 100,
  fields: 'id,name,type,mime_type,updated_at,permissions'
});

Example (KMS):

const pages = await sdk.kms.listKmsPages({
  connectionId,
  limit: 100,
  fields: 'id,title,space_id,updated_at'
});

List endpoints return metadata, not contract bodies.

Full content must be retrieved explicitly.

Step 4: Retrieve Full Contract Content

Storage:

const file = await sdk.storage.getStorageFile({
  connectionId,
  id: fileId,
  fields: 'download_url'
});

KMS:

const page = await sdk.kms.getKmsPage({
  connectionId,
  id: pageId,
  fields: 'download_url'
});

Fetch the content using the returned download_url.

Unified.to does not inline contract bodies.

Step 5: Chunk Contracts for RAG

Legal RAG quality depends on chunking.

Chunk by:

  • Section headings
  • Clause boundaries
  • Defined term groupings
  • Structured paragraphs

Attach metadata to each chunk:

  • connection_id
  • object_type
  • object_id
  • updated_at
  • permissions (for files)
  • space_id / parent_id

Unified.to normalizes the schema. You manage embedding and vector storage.

Step 6: Store in Your Vector Database

Unified.to does not:

  • Store embeddings
  • Cache payloads
  • Maintain vector indexes

Store embeddings in your infrastructure.

Use metadata to support:

  • Tenant isolation
  • Permission enforcement
  • Update replacement
  • Space/folder scoping

Step 7: Permission-Aware RAG Retrieval

When a user queries:

  1. Embed the query.
  2. Retrieve top-matching chunks.
  3. Filter by:
    • connection_id
    • permissions[] (for files)
    • Application-level user rules (for pages)
  4. Pass filtered context to the model.
  5. Generate response.

File objects include explicit permission metadata.

KMS objects do not.

Your RAG system must enforce access before generation.

Webhook-Driven RAG vs Static Indexing

Legal RAG systems require up-to-date contracts.

Use:

  • Webhooks (native, or virtual at your interval) for ingestion
  • updated_gte for incremental sync, on supported integrations
  • Re-chunking and re-embedding on updates

RAG without synchronization leads to stale contract answers.

Unified.to supports event-driven ingestion but does not manage embeddings.

Use RAG when you need:

  • Clause-level search
  • Renewal tracking
  • Cross-repository contract discovery
  • AI assistants grounded in agreement text
  • Permission-aware contract Q&A

RAG is appropriate when retrieval is required.

It is not a substitute for legal interpretation.

Unified.to is not 'a RAG tool.'

It is:

  • The normalized retrieval layer
  • The tenant-isolated connection layer
  • Webhook-driven updates on supported integrations
  • File permission metadata, on supported integrations

Legal RAG systems require:

  • Request-time reads from the source
  • Stable schemas across providers
  • Explicit permission metadata
  • Deterministic tenant scoping

Unified.to provides those foundations.

You own embeddings, indexing, and generation.

Closing Thoughts

RAG works for legal insight only when retrieval boundaries are clear.

Unified.to's Storage and Knowledge Management APIs provide:

  • Normalized contract objects
  • Request-time reads from the source
  • Webhook-driven updates on supported integrations
  • Explicit file permission metadata
  • Connection-level tenant isolation

That clarity makes RAG defensible.

→ Start your 30-day free trial

→ Book a demo

All articles