Skip to main content
The Audit Logs API enables you to record security-relevant events in your application and export them for compliance and analysis purposes.

Create Event

Records an audit log event for an organization.
await workos.auditLogs.createEvent(
  'org_01HEZYMVP4E1Q5QFZGS4Z0WM25',
  {
    action: 'user.login_succeeded',
    occurredAt: new Date(),
    actor: {
      id: 'user_01HEZYMVP4E1Q5QFZGS4Z0WM25',
      name: 'Jane Doe',
      type: 'user',
      metadata: {
        role: 'admin'
      }
    },
    targets: [
      {
        id: 'resource_123',
        name: 'Production Database',
        type: 'database'
      }
    ],
    context: {
      location: '192.168.1.1',
      userAgent: 'Mozilla/5.0'
    },
    metadata: {
      success: true,
      method: 'password'
    }
  },
  {
    idempotencyKey: 'unique-event-key-123'
  }
);

Parameters

organization
string
required
The ID of the organization to create the audit log event for.
event
object
required
The audit log event details.
options
object
Request options.

Response

Returns a Promise that resolves to void when the event is successfully created.

Create Export

Creates an export of audit log events matching the specified criteria.
const auditLogExport = await workos.auditLogs.createExport({
  organizationId: 'org_01HEZYMVP4E1Q5QFZGS4Z0WM25',
  rangeStart: new Date('2024-01-01'),
  rangeEnd: new Date('2024-01-31'),
  actions: ['user.login_succeeded', 'user.login_failed'],
  actorNames: ['Jane Doe'],
  actorIds: ['user_01HEZYMVP4E1Q5QFZGS4Z0WM25'],
  targets: ['database']
});

Parameters

options
object
required
organizationId
string
required
The ID of the organization to export audit logs for.
rangeStart
Date
required
Start date for the export range.
rangeEnd
Date
required
End date for the export range.
actions
string[]
Filter by specific action types.
actorNames
string[]
Filter by actor names.
actorIds
string[]
Filter by actor IDs.
targets
string[]
Filter by target types.

Response

object
string
Always audit_log_export.
id
string
Unique identifier for the export.
state
string
Current state of the export: pending, ready, or error.
url
string
Download URL for the export. Available when state is ready.
createdAt
string
ISO 8601 timestamp when the export was created.
updatedAt
string
ISO 8601 timestamp when the export was last updated.

Get Export

Retrieves the status and details of an audit log export.
const auditLogExport = await workos.auditLogs.getExport(
  'audit_log_export_01HEZYMVP4E1Q5QFZGS4Z0WM25'
);

if (auditLogExport.state === 'ready') {
  console.log('Download URL:', auditLogExport.url);
}

Parameters

auditLogExportId
string
required
The ID of the audit log export to retrieve.

Response

object
string
Always audit_log_export.
id
string
Unique identifier for the export.
state
string
Current state of the export: pending, ready, or error.
url
string
Download URL for the export. Available when state is ready.
createdAt
string
ISO 8601 timestamp when the export was created.
updatedAt
string
ISO 8601 timestamp when the export was last updated.

Create Schema

Defines a schema for an audit log action type to validate event structure.
const schema = await workos.auditLogs.createSchema(
  {
    action: 'document.shared',
    targets: [
      {
        type: 'document',
        metadata: {
          file_size: { type: 'number' },
          encrypted: { type: 'boolean' }
        }
      },
      {
        type: 'user'
      }
    ],
    actor: {
      metadata: {
        department: { type: 'string' }
      }
    },
    metadata: {
      share_type: { type: 'string' },
      expiration_days: { type: 'number' }
    }
  },
  {
    idempotencyKey: 'schema-creation-key-123'
  }
);

Parameters

schema
object
required
action
string
required
The action name this schema applies to.
targets
array
required
Schema definitions for target resources.
actor
object
Schema definition for the actor.
metadata
object
Schema for event-level metadata fields.
options
object
Request options.

Response

object
string
Always audit_log_schema.
version
number
Version number of the schema.
targets
array
Array of target schema definitions.
actor
object
Actor schema definition.
metadata
object
Event-level metadata schema definition.
createdAt
string
ISO 8601 timestamp when the schema was created.

Build docs developers (and LLMs) love