# Reactions and views

> Like or dislike an article, read the totals, and count a read.

URL: https://renzo-digital.com/docs/resources/engagement

Reactions are a like or a dislike on an article. Views count how many readers opened it. Anyone may read reaction totals and report a view. Changing a reaction needs a token and a username.

## Endpoints

| Operation | What it does | Auth |
| --- | --- | --- |
| `GET /api/v1/articles/{article_id}/reactions` | Read the like and dislike totals. With a token, it also shows your own reaction. | Token optional |
| `PUT /api/v1/articles/{article_id}/reactions` | Like or dislike: `{"kind": "like"}` or `{"kind": "dislike"}`. | Token and username |
| `DELETE /api/v1/articles/{article_id}/reactions` | Remove your reaction. | Token and username |
| `POST /api/v1/articles/{article_id}/views` | Report one read. Answers `202`. | None |

`article_id` is the article's `id`, not its slug. Calls that change a reaction need a username on the account. Until one is set, they answer `409` [`profile_incomplete`](https://renzo-digital.com/docs/error-codes#profile_incomplete). See [Account](https://renzo-digital.com/docs/resources/account). Any mutating call accepts an `Idempotency-Key`. See [Idempotency](https://renzo-digital.com/docs/concepts/idempotency).

## Example: like an article

```bash tab="curl"
curl -X PUT "$BASE/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/reactions" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"kind": "like"}'
```

```python tab="Python"
import os

import requests

BASE = os.environ["BASE"]
TOKEN = os.environ["TOKEN"]

answer = requests.put(
    f"{BASE}/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/reactions",
    headers={"Authorization": f"Bearer {TOKEN}"},
    json={"kind": "like"},
)
answer.raise_for_status()
print(answer.json())
```

```js tab="Node.js"
const { BASE, TOKEN } = process.env;

const answer = await fetch(`${BASE}/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/reactions`, {
  method: "PUT",
  headers: { Authorization: `Bearer ${TOKEN}`, "Content-Type": "application/json" },
  body: JSON.stringify({ kind: "like" }),
});
if (!answer.ok) throw new Error(`HTTP ${answer.status}`);
console.log(await answer.json());
```

The answer is `200` with the new totals:

```json title="Response"
{
  "article_id": "f43679d3-0ab5-5f68-8bda-f0a4c5560594",
  "kind": "like",
  "like_count": 128,
  "dislike_count": 7
}
```

Report a read after the reader stays a few seconds. No token is needed:

```bash tab="curl"
curl -X POST "$BASE/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/views" \
  -H "Content-Type: application/json" \
  -d '{"session_id": "b7e2c9a41f6d4e08", "locale": "en"}'
```

```python tab="Python"
import os

import requests

BASE = os.environ["BASE"]

answer = requests.post(
    f"{BASE}/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/views",
    json={"session_id": "b7e2c9a41f6d4e08", "locale": "en"},
)
answer.raise_for_status()
print(answer.json())
```

```js tab="Node.js"
const { BASE } = process.env;

const answer = await fetch(`${BASE}/api/v1/articles/f43679d3-0ab5-5f68-8bda-f0a4c5560594/views`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ session_id: "b7e2c9a41f6d4e08", locale: "en" }),
});
if (!answer.ok) throw new Error(`HTTP ${answer.status}`);
console.log(await answer.json());
```

The answer is `202`:

```json title="Response"
{
  "recorded": true
}
```

## Rules and edge cases

- **Totals.** `like_count` and `dislike_count` are exact and cover every language of the article. They are never cached.
- **Your reaction.** `kind` is `like`, `dislike` or `null` when you have none. Without a token, `kind` is always `null`.
- **Same reaction twice.** It changes nothing and still answers `200`. Sending the other kind switches your reaction.
- **Removing.** Removing a reaction that is not there is not an error. It answers `200` with the totals.
- **Gating.** To react, you must be able to open the article in full. If your plan only gets the preview, `PUT` and `DELETE` answer `403` [`article_access_required`](https://renzo-digital.com/docs/error-codes#article_access_required). Reading totals has no such check. See [Plans and limits](https://renzo-digital.com/docs/concepts/plans-and-limits).
- **Unpublished articles.** A draft, scheduled, archived or unknown ID answers `404` [`not_found`](https://renzo-digital.com/docs/error-codes#not_found).
- **View body.** `session_id` is any opaque string you make up for the session, 8 to 128 characters. `locale` is the article locale the reader saw, as the engine code or the public tag. An unsupported `locale` answers `422` [`validation_failed`](https://renzo-digital.com/docs/error-codes#validation_failed).
- **Deduplication.** The same `session_id` on the same article counts once. A repeat answers `202` with `recorded: false`.
- **No identity.** A view is anonymous. Do not send it before the reader has stayed a few seconds.

## Errors

| Code | Status | When |
| --- | --- | --- |
| [`article_access_required`](https://renzo-digital.com/docs/error-codes#article_access_required) | 403 | Your plan does not open this article, so you cannot react. |
| [`profile_incomplete`](https://renzo-digital.com/docs/error-codes#profile_incomplete) | 409 | The account has no username yet. |
| [`not_found`](https://renzo-digital.com/docs/error-codes#not_found) | 404 | No published article has this ID. |

Sign-in and request errors, such as [`token_expired`](https://renzo-digital.com/docs/error-codes#token_expired) and [`validation_failed`](https://renzo-digital.com/docs/error-codes#validation_failed), can come from any call. See [Errors](https://renzo-digital.com/docs/concepts/errors).

## Reference

- [Engagement in the API reference](https://renzo-digital.com/docs/api#tag/engagement)
