# API Reference

**Complete API documentation for versioning functions and hooks**

## Database Functions

### create_relational_version()
Trigger function - automatically creates versions on entity INSERT/UPDATE.

### get_version_diff(entity_type, from_version_id, to_version_id)
Returns JSONB diff between two versions.

### cleanup_old_versions(entity_type, keep_versions)
Deletes old versions, keeping N most recent per entity.

### rollback_to_version(entity_type, entity_id, target_version_id, changed_by, reason)
Restores entity to previous version, creates new version with change_type='restored'.

## React Hooks

### useEntityVersions(entityType, entityId)
Returns: `{ versions, currentVersion, loading, rollbackToVersion, ... }`

### useVersionComparison(entityType, fromVersionId, toVersionId)
Returns: `{ diff, loading, error }`

## Components

- `<VersionIndicator />` - Version badge
- `<EntityVersionHistory />` - Full timeline
- `<VersionComparisonDialog />` - Side-by-side diff
- `<RollbackDialog />` - Restore confirmation

See [FRONTEND.md](./FRONTEND.md) for detailed usage.
