> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gmail history capture

> Track Gmail message metadata and observed label changes.

Mount `@bijectionhq/app-imap` in Gmail mode for one mailbox address. Pass one
protected label to the receiver binder and as the app's `data`. The same label protects history positions, message metadata and Files.
[Installer-bound labels](https://docs.bijection.com/integrations/native-connectors.md#installer-bound-labels)

## Mount and start

```ts bijection/app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, secret } from "@bijectionhq/bijection";
import mailbox, { gmailHistoryReceiver } from "@bijectionhq/app-imap";

const inboxes = label();
const token = secret();
const gmail = gmailHistoryReceiver({ kind: "bearer", secret: token }, inboxes);

export default app({ inboxes, token, gmail, mail: mailbox({ data: inboxes, gmail }) });
```

Grant `mail.operator` and call `mail.source.initialize` with the reviewed
starting history ID:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{ profile: { mode: "gmail", user: "reader@example.com", startHistoryId: "1234567" } }
```

Read metadata with `mail.metadata.list`, content with `mail.content.get`
and the position with `mail.source.progress`.

Set a Gmail OAuth access token with `bijection secret set token`.
The credential needs access to the selected address and the
`https://www.googleapis.com/auth/gmail.readonly` scope. The receiver also
accepts a reviewed `oauth2` credential with a refresh token. Get the starting
`historyId` from `users.getProfile` for that address.
[Gmail History API](https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.history/list)

## Position and limits

Capture starts **after** the reviewed history ID. Gmail message IDs key the
rows within this mounted mailbox. `payload` is JSON text containing the
observed thread ID and label membership learned from history. Label removal
withdraws a label from the observed set. Trash is a label change. A
`messagesDeleted` entry withdraws the row. Messages before the seed appear
only if later history names them. The observed label set may be partial.

Each request asks for up to 20 history records. The app admits at most 256
changes per page within the 64 KiB encoded result budget and up to 512
observed labels per message. It keeps the decimal starting ID and opaque
page token while paging, then commits Gmail's returned history ID with the
last page.

Use `source.progress` to read the revision and gap stamp. `source.poll`,
`source.pause` and `source.resume` take the current revision. On a retained
position's HTTP 404, `source.reset` takes `revision`, `gapAt` and a fresh
`startHistoryId`. The prior published rows remain visible until the first
complete round from the new seed. That reset discards continuity across the
gap. Use `metadata.list` with the page's `next` as `cursor` to read the next page
of up to 100 rows.

A new baseline has one day to finish, including time spent paused. An
expired baseline keeps the previous view. Reset the source and resume it
if it is paused.

## Message Files

The app queues the raw message when history first names its immutable
message ID. It requests `format=raw` and checks the returned ID. The raw
MIME File and embedded attachment Files are available through `content.get`
by message key. Call `content.requestAttachment` with the message key and
a Gmail attachment `id` to retrieve that part directly.

Content acquisition runs separately from history. A failed read leaves the
metadata landed. `content.retry` queues a fresh raw read for a refused
capture. The raw MIME and up to 32 attachment Files share an 8 MiB budget.
The receiver streams one bounded response under the original send
permit. It decodes the base64 envelope into private 512 KiB File parts,
checks the returned identity and digest, and returns the completed
manifest. Publication verifies the digest again and retires the parts
in the same commit.
[Gmail raw messages](https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.messages)
· [Gmail attachment reads](https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.messages.attachments/get)

## Mailbox label definitions

Mount `@bijectionhq/app-rest` and bind `providerEndpoint` to
`https://gmail.googleapis.com` and `/gmail/v1/users/me/labels` for the
authorized mailbox. Seal a bearer or OAuth2 credential with Gmail label
access. Pass the same protected label to the receiver binder and as the app's `data`.

Initialize with
`{ profile: { mode: "provider", provider: "gmail.labels.observations", limit: 100 } }`.
The app publishes Gmail's system and user label definitions by label ID.
The API returns a single response, bounded to 100 definitions and 36 KiB.
The result has no cursor and missing definitions remain published.

[List labels](https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.labels/list)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.