An Audit Log Event represents a notable action taken within your application. Each event captures what happened, who did it, what was affected, and contextual information about when and where it occurred.
Create an Audit Log Event.
This API supports idempotency which guarantees that performing the same operation multiple times will have the same result as if the operation were performed only once. This is handy in situations where you may need to retry a request due to a failure or prevent accidental duplicate requests from creating more than one resource.
To achieve idempotency, you can add Idempotency-Key request header to a Create Event request with a unique string as the value. Each subsequent request matching this unique string will return the same response. We suggest using v4 UUIDs for idempotency keys to avoid collisions.
Idempotency keys expire after 24 hours. The API will generate a new response if you submit a request with an expired key.
curl --request POST \ --url https://api.workos.com/audit_logs/events \ --header "Authorization: Bearer sk_example_123456789" \ --header "Content-Type: application/json" \ --header "Idempotency-Key: 884793cd-bef4-46cf-8790-e3d4957a09ce" \ -d @- <<BODY { "organization_id": "org_01EHWNCE74X7JSDV0X3SZ3KJNY", "event": { "action": "user.signed_in", "occurred_at": "2022-09-02T16:35:39.317Z", "version": 1, "actor": { "type": "user", "id": "user_TF4C5938", "metadata": { "role": "admin" } }, "targets": [ { "type": "user", "id": "user_98432YHF", "name": "Jon Smith" }, { "type": "team", "id": "team_J8YASKA2", "metadata": { "owner": "user_01GBTCQ2" } } ], "context": { "location": "123.123.123.123", "user_agent": "Chrome/104.0.0.0" }, "metadata": { "extra": "data" } } } BODY
import { WorkOS } from '@workos-inc/node'; const workos = new WorkOS('sk_example_123456789'); await workos.auditLogs.createEvent( 'org_01EHWNCE74X7JSDV0X3SZ3KJNY', { action: 'user.signed_in', occurredAt: new Date(), version: 1, actor: { type: 'user', id: 'user_TF4C5938', name: 'Jon Smith', metadata: { role: 'admin', }, }, targets: [ { type: 'user', id: 'user_98432YHF', name: 'Jon Smith', }, { type: 'team', id: 'team_J8YASKA2', metadata: { owner: 'user_01GBTCQ2', }, }, ], context: { location: '1.1.1.1', userAgent: 'Chrome/104.0.0.0', }, metadata: { extra: 'data', }, }, { idempotencyKey: '884793cd-bef4-46cf-8790-ed49257a09c6', }, );
require "workos" WorkOS.configure do |config| config.key = "sk_example_123456789" end event = { action: "user.signed_in", occurred_at: "2022-09-08T19:46:03.435Z", version: 1, actor: { id: "user_TF4C5938", type: "user", name: "Jon Smith", metadata: { role: "admin" } }, targets: [ { id: "user_98432YHF", type: "user", name: "Jon Smith" }, { id: "team_J8YASKA2", type: "team", metadata: { owner: "user_01GBTCQ2" } } ], context: { location: "1.1.1.1", user_agent: "Chrome/104.0.0.0" }, metadata: { extra: "data" } } WorkOS::AuditLogs.create_event( organization: "org_01EHWNCE74X7JSDV0X3SZ3KJNY", event: event, idempotency_key: "884793cd-bef4-46cf-8790-ed49257a09c6" )
import datetime from workos import WorkOSClient from workos.types.audit_logs import AuditLogEvent workos_client = WorkOSClient( api_key="sk_example_123456789", client_id="client_123456789" ) event: AuditLogEvent = { "action": "user.signed_in", "occurred_at": datetime.datetime.now(tz=datetime.UTC).isoformat(), "version": 1, "actor": { "id": "user_123", "type": "user", "metadata": {"role": "admin"}, }, "targets": [ { "type": "user", "id": "user_98432YHF", "name": "Jon Smith", }, { "type": "team", "id": "team_J8YASKA2", "metadata": { "owner": "user_01GBTCQ2", }, }, ], "context": { "location": "123.123.123.123", "user_agent": "Chrome/104.0.0.0", }, "metadata": {"extra": "data"}, } workos_client.audit_logs.create_event( organization_id="org_01EHWNCE74X7JSDV0X3SZ3KJNY", event=event, idempotency_key="884793cd-bef4-46cf-8790-ed49257a09c6", )
package main import ( "context" "time" "github.com/workos/workos-go/v3/pkg/auditlogs" ) func main() { auditlogs.SetAPIKey("sk_example_123456789") err := auditlogs.CreateEvent(context.Background(), auditlogs.CreateEventOpts{ OrganizationID: "org_01EHWNCE74X7JSDV0X3SZ3KJNY", Event: auditlogs.Event{ Action: "user.signed_in", OccurredAt: time.Now(), Version: 1, Actor: auditlogs.Actor{ ID: "user_TF4C5938", Type: "user", Name: "Jon Smith", Metadata: map[string]interface{}{ "role": "admin", }, }, Targets: []auditlogs.Target{ { Type: "user", ID: "user_98432YHF", Name: "Jon Smith", }, { Type: "team", ID: "team_J8YASKA2", Metadata: map[string]interface{}{ "owner": "user_01GBTCQ2", }, }, }, Context: auditlogs.Context{ Location: "1.1.1.1", UserAgent: "Chrome/104.0.0.0", }, Metadata: map[string]interface{}{ "extra": "data", }, }, IdempotencyKey: "884793cd-bef4-46cf-8790-ed49257a09c6", }) }
<?php WorkOS\WorkOS::setApiKey("sk_example_123456789"); $auditLogs = new WorkOS\AuditLogs(); $auditLogEvent = [ "action" => "user.signed_in", "occurred_at" => date("c"), "version" => 1, "actor" => [ "id" => "user_TF4C5938", "type" => "user", "name" => "Jon Smith", "metadata" => [ "role" => "admin", ], ], "targets" => [ [ "id" => "user_98432YHF", "type" => "user", "name" => "Jon Smith", ], [ "id" => "user_98432YHF", "type" => "team", "metadata" => [ "owner" => "user_01GBTCQ2", ], ], ], "context" => [ "location" => "1.1.1.1", "user_agent" => "Chrome/104.0.0.0", ], "metadata" => [ "extra" => "data", ], ]; $idempotencyKey = "884793cd-bef4-46cf-8790-ed49257a09c6"; $auditLogs->createEvent( organizationId: "org_01EHWNCE74X7JSDV0X3SZ3KJNY", event: $auditLogEvent, idempotencyKey: $idempotencyKey );
import com.workos.WorkOS; import com.workos.auditlogs.AuditLogsApi.CreateAuditLogEventOptions; import com.workos.auditlogs.AuditLogsApi.CreateAuditLogEventRequestOptions; import java.util.Date; import java.util.Map; WorkOS workos = new WorkOS("sk_example_123456789"); CreateAuditLogEventOptions options = CreateAuditLogEventOptions.builder() .action("user.signed_in") .occurredAt(new Date()) .actor("user_TF4C5938", "user", "Jon Smith", Map.of("role", "admin")) .target("user_98432YHF", "user", "Jon Smith") .target("team_J8YASKA2", "team", null, Map.of("owner", "user_01GBTCQ2")) .context("1.1.1.1", "Chrome/104.0.0.0") .version(1) .metadata(Map.of("extra", "data")) .build(); CreateAuditLogEventRequestOptions reqOptions = CreateAuditLogEventRequestOptions.builder() .idempotencyKey("884793cd-bef4-46cf-8790-ed49257a09c6") .build(); workos.auditLogs.createEvent("org_01EHWNCE74X7JSDV0X3SZ3KJNY", options, requestOptions);
WorkOS.SetApiKey("sk_example_123456789"); var auditLogsService = new AuditLogsService(); var auditLogEvent = new AuditLogEvent { Action = "user.signed_in", OccurredAt = DateTime.Now, Version = 1, Actor = new AuditLogEventActor { Id = "user_TF4C5938", Type = "user", Name = "Jon Smith", Metadata = new Dictionary<string, string> { { "role", "admin" }, }, }, Targets = new List<AuditLogEventTarget>() { new AuditLogEventTarget { Id = "user_98432YHF", Type = "user", Name = "Jon Smith", }, new AuditLogEventTarget { Id = "team_J8YASKA2", Type = "team", Metadata = new Dictionary<string, string> { { "owner", "user_01GBTCQ2" }, }, }, }, Context = new AuditLogEventContext { Location = "1.1.1.1", UserAgent = "Chrome/104.0.0.0", }, Metadata = new Dictionary<string, string> { { "extra", "data" }, }, }; var idempotencyKey = "884793cd-bef4-46cf-8790-ed49257a09c6"; var organizationId = "org_01EHWNCE74X7JSDV0X3SZ3KJNY"; var options = new CreateAuditLogEventOptions() { OrganizationId = organizationId, Event = auditLogEvent }; auditLogsService.CreateEvent(options, idempotencyKey);
POST/audit_logs /eventsParameters Returns