# Notebooks and comments

Write notebooks, discuss them in comment threads and count the comments on a notebook or dashboard.

Use notebooks and comments to test code that writes analysis notes to PostHog and follows the discussion around them. The example below writes a notebook, leaves one comment on it and counts that notebook's comments:

```bash
KEY=phx_rysticexamplepersonalkey000000000000000
API="$RYSTIC_URL/api/projects/3104"
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X POST "$API/notebooks/" -d '{"title":"Checkout funnel review","content":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Drop-off at shipping."}]}]}}' | jq -c '{short_id, title, version, text_content, created_by: .created_by.email}'
# -> {"short_id":"A0000001","title":"Checkout funnel review","version":0,"text_content":"Drop-off at shipping.","created_by":"sam.adeyemi@hogflix.example"}
FIRST=$(curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X POST "$API/comments/" -d '{"content":"Is the drop-off real?","scope":"Notebook","item_id":"A0000001"}' | jq -r .id)
curl -s -H "Authorization: Bearer $KEY" "$API/comments/$FIRST/" | jq -c '{scope, item_id, content, source_comment, created_by: .created_by.email}'
# -> {"scope":"Notebook","item_id":"A0000001","content":"Is the drop-off real?","source_comment":null,"created_by":"sam.adeyemi@hogflix.example"}
curl -s -H "Authorization: Bearer $KEY" "$API/comments/count/?scope=Notebook&item_id=A0000001"
# -> {"count":1}
```

A comment points at what it discusses through two fields. `scope` names the kind of item, such as `Notebook` or `Dashboard`, and `item_id` names the item itself. For a notebook, `item_id` is the notebook's `short_id`.

## Update a notebook

To change a notebook, send a `PATCH` to its `short_id` with the fields to replace and the `version` you last read. Reading it back returns the new title, while `text_content` keeps the text written earlier:

```bash
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X PATCH "$API/notebooks/A0000001/" -d '{"title":"Checkout funnel, week 9","version":0}' -o /dev/null -w '%{http_code}\n'
# -> 200
curl -s -H "Authorization: Bearer $KEY" "$API/notebooks/A0000001/" | jq -c '{title, version, text_content}'
# -> {"title":"Checkout funnel, week 9","version":0,"text_content":"Drop-off at shipping."}
curl -s -H "Authorization: Bearer $KEY" "$API/notebooks/" | jq -c '{count, titles: [.results[].title]}'
# -> {"count":1,"titles":["Checkout funnel, week 9"]}
```

The simulator stores the `version` your update sends instead of raising it, so the read above still shows `0`. Check conflict handling against PostHog; [Limitations](https://www.rystic.ai/docs/simulators/posthog.md#limitations) explains why.

## Reply to a comment and read the thread

To reply, post a comment on the same item and set `source_comment` to the `id` of the comment you are replying to. Reading `/comments/{id}/thread/` on the first comment then lists its replies:

```bash
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X POST "$API/comments/" -d "{\"content\":\"Yes, on mobile.\",\"scope\":\"Notebook\",\"item_id\":\"A0000001\",\"source_comment\":\"$FIRST\"}" | jq -c '{content, source_comment: (.source_comment == "'"$FIRST"'")}'
# -> {"content":"Yes, on mobile.","source_comment":true}
curl -s -H "Authorization: Bearer $KEY" "$API/comments/$FIRST/thread/" | jq -c '{count, replies: [.results[].content]}'
# -> {"count":1,"replies":["Yes, on mobile."]}
curl -s -H "Authorization: Bearer $KEY" "$API/comments/?scope=Notebook&item_id=A0000001" | jq -c '[.results[].content]'
# -> ["Yes, on mobile.","Is the drop-off real?"]
```

The thread includes only the replies, not the comment that opened it. Filtering the comment list by `scope` and `item_id` returns the whole discussion on one item, newest first.

## Edit and delete a comment

To edit a comment, send a `PATCH` with the new `content`. Each edit raises the comment's `version` by one. To delete a comment, send `{"deleted": true}` in a `PATCH`; it then drops out of its thread and out of the count:

```bash
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X PATCH "$API/comments/$FIRST/" -d '{"content":"Is the drop-off real, or a tracking gap?"}' | jq -c '{content, version}'
# -> {"content":"Is the drop-off real, or a tracking gap?","version":1}
REPLY=$(curl -s -H "Authorization: Bearer $KEY" "$API/comments/$FIRST/thread/" | jq -r '.results[0].id')
curl -s -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' -X PATCH "$API/comments/$REPLY/" -d '{"deleted":true}' | jq -c '{deleted}'
# -> {"deleted":true}
curl -s -H "Authorization: Bearer $KEY" "$API/comments/$FIRST/thread/" | jq -c .count
# -> 0
curl -s -H "Authorization: Bearer $KEY" "$API/comments/count/?scope=Notebook&item_id=A0000001"
# -> {"count":1}
```

## Start from a seeded discussion

To test code that reads an existing discussion, load the `posthog_identify_handoff` scenario. It seeds one notebook and one dashboard, with two comments on the notebook and one on the dashboard. Two team members wrote them, and the project id in this scenario is 3102:

```bash
curl -s -X POST "$RYSTIC_URL/_rystic/scenario" -o /dev/null -w '%{http_code}\n' -d '{"name":"posthog_identify_handoff"}'
# -> 204
SEEDED="$RYSTIC_URL/api/projects/3102"
curl -s -H "Authorization: Bearer $KEY" "$SEEDED/notebooks/" | jq -c '[.results[] | {short_id, title}]'
# -> [{"short_id":"hNd0ff7Q","title":"Anonymous to identified handoff"}]
curl -s -H "Authorization: Bearer $KEY" "$SEEDED/comments/?scope=Notebook&item_id=hNd0ff7Q" | jq -c '[.results[] | {author: .created_by.first_name, reply: (.source_comment != null)}]'
# -> [{"author":"Mina","reply":true},{"author":"Theo","reply":false}]
curl -s -H "Authorization: Bearer $KEY" "$SEEDED/comments/?scope=Dashboard" | jq -c '[.results[] | {author: .created_by.first_name, item_id}]'
# -> [{"author":"Theo","item_id":"12"}]
```

Mina's comment is a reply to Theo's, so reading Theo's thread returns it. Loading a scenario replaces every notebook and comment, including those written earlier on this page.

## Reference

- A fresh simulator assigns notebook `short_id` values in order, starting at `A0000001`.
- `created_by` on a new notebook or comment is the owner of the personal API key that sent it.
- Timestamps come from the simulator's clock, which each scenario sets ([Reset and determinism](https://www.rystic.ai/docs/concepts/reset-and-determinism.md)).
- Read only the basic user fields from `created_by`; [Limitations](https://www.rystic.ai/docs/simulators/posthog.md#limitations) explains why.

---

Source: https://www.rystic.ai/docs/simulators/posthog/features/notebooks-and-comments · Markdown: https://www.rystic.ai/docs/simulators/posthog/features/notebooks-and-comments.md
