<!-- llms.txt: https://workos.com/llms.txt -->
<!-- feedback.md: https://workos.com/docs/feedback.md -->

> Agents: if this page is wrong, missing a step, or its code fails, report it instead of working around it. `POST https://api.workos.com/feedback` with `Content-Type: application/json` and body `{"message":"Agent report: what you tried, what happened, client and model","pageUrl":"https://workos.com/docs/audit-logs/exporting-events"}`. Ask the user once first. No auth. No secrets. Details: https://workos.com/docs/feedback.md

# Exporting Events

## Exporting Events

You may need to export Audit Log Events in large chunks. WorkOS supports exporting events as CSV files through both the Dashboard and API.

Exports are scoped to a single organization within a specified date range. Events from the past three months can be included in the export. You may define additional filters such as `actions`, `actors`, and `targets`.

### Creating an export through the Dashboard

Exports can be manually created under the Organization page when viewing Audit Log Events by selecting "Export CSV" from the "Actions" dropdown. Set your filters and select "Generate CSV file".

![A screenshot showing how to generate an Audit Log export in the WorkOS Dashboard.](https://images.workoscdn.com/images/a5386939-652f-4cbb-aa88-7159e2ffc1dd.png?auto=format\&fit=clip\&q=50)

### Creating an export through the API

:::code-group{title="Create an Export"}

```js language="js"
import { WorkOS } from '@workos-inc/node';

const workos = new WorkOS('sk_example_123456789');

const auditLogExport = await workos.auditLogs.createExport({
  organizationId: 'org_01EHWNCE74X7JSDV0X3SZ3KJNY',
  rangeStart: new Date('2022-08-31T15:51:23.604Z'),
  rangeEnd: new Date('2022-08-31T15:51:23.604Z'),
});

// Use auditLogExport.id for fetching export at a later time
```

```rb language="ruby"
require "workos"

workos = WorkOS::Client.new(api_key: "sk_example_123456789")

audit_log_export = workos.audit_logs.create_export(
  organization_id: "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
  range_start: "2022-06-22T15:04:19.704Z",
  range_end: "2022-08-22T15:04:19.704Z"
)

# Use audit_log_export.id for fetching export at a later time
```

```py language="python"
from workos import WorkOSClient

workos_client = WorkOSClient(
    api_key="sk_example_123456789", client_id="client_123456789"
)

workos_client.audit_logs.create_export(
    organization_id="org_01EHWNCE74X7JSDV0X3SZ3KJNY",
    range_start="2022-08-31T15:51:23.604Z",
    range_end="2022-08-31T15:51:23.604Z",
)
```

```go language="go"
package main

import (
	"context"
	workos "github.com/workos/workos-go/v10"
)

func main() {
	client := workos.NewClient("sk_example_123456789")

	export, err := client.AuditLogs().CreateExport(context.Background(), &workos.AuditLogsCreateExportParams{
		OrganizationID: "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
		RangeStart:     "2022-08-31T15:51:23.604Z",
		RangeEnd:       "2022-08-31T15:51:23.604Z",
	})

	if err != nil {
		// Handle the error...
	}

	// Use export.ID for fetching export at a later time
	_ = export.ID
}
```

```php language="php"
<?php

$workos = new \WorkOS\WorkOS();

$auditLogExport = $workos
    ->auditLogs()
    ->createExport(
        organizationId: "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
        rangeStart: "2022-06-22T15:51:23.604Z",
        rangeEnd: "2022-08-31T15:51:23.604Z",
    );

// Use $auditLogExport->id for fetching export at a later time
```

```php language="laravel"
<?php

use WorkOS\Laravel\Facades\WorkOS;

$auditLogExport = WorkOS::auditLogs()->createExport(
    organizationId: "org_01EHWNCE74X7JSDV0X3SZ3KJNY",
    rangeStart: "2022-06-22T15:51:23.604Z",
    rangeEnd: "2022-08-31T15:51:23.604Z",
);

// Use $auditLogExport->id for fetching export at a later time
```

```java language="java"
import com.workos.WorkOS;
import com.workos.auditlogs.AuditLogsApi.CreateAuditLogExportOptions;
import com.workos.auditlogs.models.AuditLogExport;
import java.util.Date;

WorkOS workos = new WorkOS("sk_example_123456789");

Date rangeStart = new Date();
rangeStart.setMonth(rangeStart.getMonth() - 3);

Date rangeEnd = new Date();

CreateAuditLogExportOptions options = CreateAuditLogExportOptions.builder()
                                          .organizationId("org_123")
                                          .rangeStart(rangeStart)
                                          .rangeEnd(rangeEnd)
                                          .build();

AuditLogExport auditLogExport = workos.auditLogs.createExport(options);

// Use auditLogExport.id for fetching export at a later time
```

```cs language="dotnet"
WorkOS.SetApiKey("sk_example_123456789");

var auditLogsService = new AuditLogsService();

var options = new CreateAuditLogExportOptions() {
    OrganizationId = "org_123",
    RangeStart = DateTime.Now.AddMonths(-3),
    RangeEnd = DateTime.Now,
};

var auditLogExport = await auditLogsService.CreateExport(options);

// Use auditLogExport.Id for fetching export at a later time
```

:::

Once the export has been created, fetch the export at a later time to access the `url` of the generated CSV file.

> The URL will expire after 10 minutes. If the export is needed again at a later time, refetching the export will regenerate the URL.

:::code-group{title="Fetch Export"}

```js language="js"
import { WorkOS } from '@workos-inc/node';

const workos = new WorkOS('sk_example_123456789');

const auditLogExport = await workos.auditLogs.getExport(
  'audit_log_export_01GBT9P815WPET6H8K0XHBACGS',
);

// auditLogExport.state `pending` or `ready`
// auditLogExport.url available once `state` is `ready`
```

```rb language="ruby"
require "workos"

workos = WorkOS::Client.new(api_key: "sk_example_123456789")

audit_log_export = workos.audit_logs.get_export(
  audit_log_export_id: "audit_log_export_01GBT9P815WPET6H8K0XHBACGS"
)
```

```py language="python"
from workos import WorkOSClient

workos_client = WorkOSClient(
    api_key="sk_example_123456789", client_id="client_123456789"
)

audit_log_export = workos_client.audit_logs.get_export(
    audit_log_export_id="audit_log_export_01GBT9P815WPET6H8K0XHBACGS"
)

# audit_log_export.state "pending" or "ready"
# audit_log_export.url available once "state" is "ready"
```

```go language="go"
package main

import (
	"context"
	workos "github.com/workos/workos-go/v10"
)

func main() {
	client := workos.NewClient("sk_example_123456789")

	export, err := client.AuditLogs().GetExport(context.Background(), "audit_log_export_01GBT9P815WPET6H8K0XHBACGS")

	if err != nil {
		// Handle the error...
	}

	// export.State `pending` or `ready`
	// export.Url available once `State` is `ready`
	_ = export
}
```

```php language="php"
<?php

$workos = new \WorkOS\WorkOS();

$auditLogExport = $workos
    ->auditLogs()
    ->getExport("audit_log_export_01GBT9P815WPET6H8K0XHBACGS");

// $auditLogExport->state `pending` or `ready`
// $auditLogExport->url available once `state` is `ready`
```

```php language="laravel"
<?php

use WorkOS\Laravel\Facades\WorkOS;

$auditLogExport = WorkOS::auditLogs()->getExport(
    "audit_log_export_01GBT9P815WPET6H8K0XHBACGS",
);

// $auditLogExport->state: `pending` or `ready`
// $auditLogExport->url: available once state is `ready`
```

```java language="java"
import com.workos.WorkOS;
import com.workos.auditlogs.models.AuditLogExport;

WorkOS workos = new WorkOS("sk_example_123456789");

AuditLogExport auditLogExport =
    workos.auditLogs.getExport("audit_log_export_01GBT9P815WPET6H8K0XHBACGS");
```

```cs language="dotnet"
WorkOS.SetApiKey("sk_example_123456789");

var auditLogsService = new AuditLogsService();

var auditLogExport = await auditLogsService.GetExport("audit_log_export_01GBT9P815WPET6H8K0XHBACGS");
```

:::

If the `state` of the export is still `pending`, poll the export until it is ready for download.
