---
title: "Import event history"
description: "Map another system's history to CloudEvents, import it with its original times and origin, resume safely, and read it back."
url: "https://agentpost.tools/docs/eventwolf/guides/import-history"
kind: guide
needs:
  - what: "The `api` helper"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
  - what: "A `dev` secret key, `key`, for a project that adopted EventWolf"
    from: "https://agentpost.tools/docs/platform/guides/keys.md"
---

# Import event history

Use this guide when moving old [events](/docs/eventwolf/concepts/events.md#imported-history) from another system into EventWolf. Imported events join your history with their original times and origin, and send nothing as they arrive. To send them, or any events already in EventWolf, to a receiver, use [Replay events](/docs/eventwolf/guides/replays.md).

Run the TypeScript blocks in order as one Node 24 script with `api` and `key` from the pages above. The secret key selects the destination project and environment. Use that environment’s key for every import and verification call.

Check your [retention config](/docs/eventwolf/reference/config.md) before importing. Event retention uses the original receipt times, so old history can be removed on the next sweep. Agentpost starts projects with event retention set to `"forever"`.

## Before you start

-   The `api` helper, from [Platform quickstart](/docs/platform/quickstart.md).
-   A `dev` secret key, `key`, for a project that adopted EventWolf, from [Create a project and issue a key](/docs/platform/guides/keys.md).

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

## 1\. Map the old records

Read a sample export from your old system. Map its stable record ID to `id`, occurrence time to `time`, and receipt time to the entry’s `receivedAt`. Put its event details in `data` and keep the same `source` for every retry of that history:

```ts
const legacyHistory = [
  {
    id: 'ticket-17',
    occurredAt: '2025-02-01T09:00:00.000Z',
    receivedAt: '2025-02-01T09:00:01.000Z',
    title: 'Ticket opened',
  },
  {
    id: 'ticket-18',
    occurredAt: '2025-02-01T09:05:00.000Z',
    receivedAt: '2025-02-01T09:05:02.000Z',
    title: 'Ticket assigned',
  },
];
const historyImport = {
  batchId: 'support-export-2025-02',
  origin: 'legacy-support',
  events: legacyHistory.map((old) => ({
    receivedAt: old.receivedAt,
    event: {
      specversion: '1.0',
      id: old.id,
      source: '/legacy/support',
      type: 'support.legacy',
      time: old.occurredAt,
      summary: old.title,
      data: { title: old.title },
    },
  })),
};
```

Map your own system’s fields to CloudEvents before importing. Keep record context such as `projectId`, `seq`, and `publishedBy` out of the envelope. If the old system has no receipt time, omit the entry’s `receivedAt`; EventWolf uses the import time and leaves `originalReceivedAt` absent.

## 2\. Import a bounded batch

Split the export into requests of at most 100 entries and 1 MB, including the envelope and provenance. Send one request at a time, saving its results before sending the next batch:

```ts
const movedHistory = await api(
  'POST',
  '/v1/events/events/import',
  key,
  historyImport,
);
console.log(movedHistory.results[0].result, movedHistory.results[1].result);
// accepted accepted
```

Each entry answers `accepted`, `duplicate`, or `rejected`, in request order. The import records which types it observed and skips the registry’s publish checks. The envelope and data size limits still apply. A rejected entry’s `code` and `issues` explain what to fix. Rejections create no ingestion failure or operational event.

## 3\. Resume with the same event IDs

Retry the same batch after a lost answer, or send the corrected rejected entries with the same batch ID and origin. Accepted entries remain unchanged:

```ts
const repeatedHistory = await api(
  'POST',
  '/v1/events/events/import',
  key,
  historyImport,
);
console.log(
  repeatedHistory.results[0].result,
  repeatedHistory.results[1].result,
);
// duplicate duplicate
```

Deduplication uses `source` and `id` while the original event is still stored in the destination project. A batch ID groups provenance. Reusing it with new event IDs still imports those events.

## Check it worked

Read the imported source newest first with `sort=receivedAt`, keeping each page’s `next` cursor as `after` for the next request. Keep the same sort, direction, and filters on every page. Then look up the original ID and get the same event by its new sequence:

```ts
const importedIds: string[] = [];
let importedAfter: string | undefined;
do {
  const search = new URLSearchParams({
    sources: '/legacy/support',
    sort: 'receivedAt',
    order: 'desc',
    limit: '1',
  });
  if (importedAfter) search.set('after', importedAfter);
  const importedPage = await api('GET', `/v1/events/events?${search}`, key);
  importedIds.push(
    ...importedPage.events.map(
      (record: { event: { id: string } }) => record.event.id,
    ),
  );
  importedAfter = importedPage.next;
} while (importedAfter);
console.log(importedIds.join(', ')); // ticket-18, ticket-17
const { event: importedRecord } = await api(
  'GET',
  '/v1/events/events/lookup?source=/legacy/support&id=ticket-17',
  key,
);
console.log(importedRecord.event.time, importedRecord.receivedAt);
// 2025-02-01T09:00:00.000Z 2025-02-01T09:00:01.000Z
console.log(importedRecord.publishedBy.via, importedRecord.imported.origin);
// import legacy-support
const { event: importedBySequence } = await api(
  'GET',
  `/v1/events/events/${importedRecord.sequence}`,
  key,
);
console.log(importedBySequence.event.id); // ticket-17
```

Omit the `sources` filter to read imported and live history together in receipt-time order. Use `order=asc` for oldest first. [History order](/docs/eventwolf/concepts/events.md#history-order) explains how this differs from the default sequence order and the live tail. [Import event history](/docs/eventwolf/reference/events.md#import-event-history) lists the request fields and the answer.
