<!-- source: https://www.secondlayer.tools/docs/print-events -->

# Print Events

You don't decode prints: every one is indexed already parsed from Clarity into JSON, with the raw hex kept beside it.

```bash
curl "https://api.secondlayer.tools/v1/index/events?event_type=print&contract_id=SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry&order=desc&limit=1" \
  -H "Authorization: Bearer sk-sl_…"
```

```ts
import { Index } from "@secondlayer/sdk";

const index = new Index();

const page = await index.events.list({
  eventType: "print",
  contractId: "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry",
  order: "desc",
  limit: 1,
});
```

```json response.json status=200
{
  "events": [
    {
      "cursor": "9348740:2",
      "block_height": 9348740,
      "block_time": "2026-10-28T17:42:12.000Z",
      "tx_id": "0xfff08aecc097d418f55b98b10abe2bc6b1785b89b0ed1b80506fc15a375a14f9",
      "tx_index": 0,
      "event_index": 2,
      "event_type": "print",
      "contract_id": "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry",
      "payload": {
        "topic": "print",
        "value": {
          "amount": "40477",
          "block-height": "973577",
          "max-fee": "850",
          "recipient": { "hashbytes": "0x98cfcc0a22d16a9ce131dd774ee0245a3b10dcfb", "version": "0x04" },
          "request-id": "4636",
          "sender": "SPEG24N6F52VPD8001H20TD317GHDD23GGVEZC56",
          "topic": "withdrawal-create"
        },
        "raw_value": "0x0c0000000706616d6f756e74…"
      }
    }
  ],
  "next_cursor": "desc:9348740:2",
  "tip": { "block_height": 9348932, "finalized_height": 9348069, "lag_seconds": 18 },
  "reorgs": []
}
```

The decoded tuple is `value`: uints are decimal strings, buffers are hex, principals are strings. `raw_value` is the hex you started from, kept so you can check the decode.

## See Each Topic's Fields

A contract prints different shapes under different topics. The print-schema call lists a contract's topics and each field's Clarity and TypeScript type.

```bash
curl "https://api.secondlayer.tools/v1/index/contracts/SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry/print-schema" \
  -H "Authorization: Bearer sk-sl_…"
```

```ts
const schema = await index.printSchema(
  "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry",
);
```

```json response.json status=200
{
  "contract_id": "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-registry",
  "topics": [
    { "topic": "completed-deposit", "count": 1943, "fields": "…" },
    {
      "topic": "withdrawal-create",
      "count": 29,
      "fields": [
        { "name": "amount", "camel_name": "amount", "clarity_type": "uint", "ts_type": "bigint" },
        { "name": "block-height", "camel_name": "blockHeight", "clarity_type": "uint", "ts_type": "bigint" },
        { "name": "max-fee", "camel_name": "maxFee", "clarity_type": "uint", "ts_type": "bigint" },
        { "name": "recipient", "camel_name": "recipient", "clarity_type": "(tuple (hashbytes (buff 32)) (version (buff 1)))", "ts_type": "{ hashbytes: string; version: string }" },
        { "name": "request-id", "camel_name": "requestId", "clarity_type": "uint", "ts_type": "bigint" },
        { "name": "sender", "camel_name": "sender", "clarity_type": "principal", "ts_type": "string" }
      ]
    },
    { "topic": "withdrawal-accept", "count": 17, "fields": "…" },
    { "topic": "withdrawal-reject", "count": 10, "fields": "…" },
    { "topic": "key-rotation", "count": 1, "fields": "…" }
  ],
  "sampled": true
}
```

Clarity ABIs don't describe prints, so the schema is inferred from indexed prints. `camel_name` is what a subgraph handler sees on `event.data`, where a uint is a bigint; REST values are decimal strings.

| Field or case | Means |
| --- | --- |
| `sampled: true` | Older topics may be missing |
| `optional_some_rate` | Reported on optional fields |
| `"*"` topic | Prints with no `topic` string, or that aren't tuples, with `non_tuple` set |
| Cache | Five minutes (`max-age=300`, weak `ETag`); an empty result is not cached |

`secondlayer subgraphs create <name> --from-contract <contract_id>` reads the same schema: [Typed print payloads](https://www.secondlayer.tools/docs/subgraphs.md#typed-print-payloads).

## Pick Out One Topic

The events feed filters by contract, not by topic. Keep the topic you want from the tuple's own `topic` key.

```ts
const withdrawals = page.events.filter(
  (event) => (event.payload.value as { topic?: string } | undefined)?.topic === "withdrawal-create",
);
```

To be pushed one topic instead of reading it, a webhook trigger takes `topic`: [Print fields](https://www.secondlayer.tools/docs/webhooks.md#print-fields).

## Tradeoff

Reading a contract's prints means reading every topic it emits, so a chatty contract costs more rows than the topic you keep. Page with [Cursors and Pages](https://www.secondlayer.tools/docs/cursors.md). Every filter and the response schema: [Decoded events by event_type](https://www.secondlayer.tools/docs/api-reference.md#list-events), [Print payload schemas](https://www.secondlayer.tools/docs/api-reference.md#get-print-schema).

## Read next

- [Subgraphs](https://www.secondlayer.tools/docs/subgraphs.md)
- [Webhooks](https://www.secondlayer.tools/docs/webhooks.md)
- [Cursors and Pages](https://www.secondlayer.tools/docs/cursors.md)
- [API](https://www.secondlayer.tools/docs/api-reference.md)

## Notes for agents

- Prints: GET /v1/index/events?event_type=print&contract_id=<id> (SDK: sl.index.events.list). Never hand-decode payload.raw_value.
- No topic filter: a topic param returns 400 'unknown query param'. payload.topic is the event's label, often just 'print'; the contract's topic is payload.value.topic. Filter client-side or use a webhook print_event trigger with 'topic'.
- Schema: GET /v1/index/contracts/<id>/print-schema (SDK: sl.index.printSchema(id), null on 404). Read it before designing tables.
- Values: tuples are objects with kebab-case Clarity keys; uint/int are decimal strings in REST, bigint in handlers.

## Setup

Ensure setup once, skipping any step already done:
- CLI: `bun add -g @secondlayer/cli`
- Instance: `secondlayer init` writes `INSTANCE_TOKEN`. Loopback `/v1` reads need no key.
- Hosted reads: export `SECONDLAYER_API_KEY` (`sk-sl_*`) or buy credits with `secondlayer credits buy` (`balance`, `refill`), then `secondlayer whoami`
