# `TwitterViewer`
[🔗](https://github.com/TwitterViewer/twitterviewer-elixir/blob/v0.1.0/lib/twitterviewer.ex#L1)

Elixir client for the [TwitterViewer API](https://twitterviewer.ai/docs/api): read public X
(Twitter) posts, threads, profiles and replies as JSON, Markdown, text or PDF. All access is
read-only.

    client = TwitterViewer.new()   # reads TWITTERVIEWER_API_KEY
    {:ok, result} = TwitterViewer.convert(client, "https://x.com/jack/status/20")
    IO.puts(result.markdown)

The API needs a Plus plan: without it every call returns an `TwitterViewer.APIError` with
status 402, code `"LIMIT_REACHED"` and `details["reason"] == "PLUS_REQUIRED"`.

Every call returns `{:ok, value}` or `{:error, error}`; see `TwitterViewer.Error`. Values that
can never work (an unknown option, a malformed handle) raise `ArgumentError` and send nothing.

## Options of the convert and swap functions

Whatever you leave out is not sent, so the server default applies. Atoms and strings are both
accepted for the values.

  * `:scope` - `:thread` (default), `:post` or `:conversation`. Post links only.
  * `:stats` - `:all` (default), `:root` or `:off`. Engagement numbers in the rendered document.
  * `:max` - profile links only: number of recent posts, 1 to 200 (default 20)
  * `:replies` - profile links only: include the account's replies
  * `:reposts` - profile links only: include reposts
  * `:paper` - `:a4` (default) or `:letter`. PDF only.
  * `:format` - depends on the function

## Retries

Every function is retried the same way, up to `:max_retries` times, on 429, 5xx (except 501)
and network errors. The wait is `retryAfterSec` when the server sends it, otherwise
exponential backoff (about 0.5 s, 1 s, 2 s, at most 8 s). A wait longer than 120 seconds is not
performed: the error is returned with `retry_after_sec` set. Retrying does not cost credits
twice: the API returns the first result again without charging it.

# `options`

```elixir
@type options() :: keyword()
```

# `result`

```elixir
@type result(value) :: {:ok, value} | {:error, TwitterViewer.Error.t()}
```

# `convert`

```elixir
@spec convert(TwitterViewer.Client.t(), String.t(), options()) ::
  result(TwitterViewer.ConvertResult.t())
```

Converts a post, thread or profile (`POST /api/v1/convert`) and returns a
`TwitterViewer.ConvertResult`.

`url` is a link to an X post or profile, or an `@handle`. Profile links return recent posts.
`:format` only changes the `markdown` field (`:obsidian`); use `convert_to_pdf/3` for PDF. See
the module documentation for the other options.

# `convert_by_query`

```elixir
@spec convert_by_query(TwitterViewer.Client.t(), String.t(), options()) ::
  result(TwitterViewer.ConvertResult.t())
```

The same conversion through `GET /api/v1/convert`.

# `convert_to_pdf`

```elixir
@spec convert_to_pdf(TwitterViewer.Client.t(), String.t(), options()) ::
  result(TwitterViewer.Document.t())
```

Converts a link to a PDF rendered by the server and returns a `TwitterViewer.Document`.

# `convert_to_text`

```elixir
@spec convert_to_text(TwitterViewer.Client.t(), String.t(), options()) ::
  result(TwitterViewer.Document.t())
```

Converts a link and returns only the document body as a `TwitterViewer.Document`.

`:format` is `:markdown` (default), `:obsidian` or `:text`.

# `credits`

```elixir
@spec credits(TwitterViewer.Client.t()) :: result(TwitterViewer.CreditBalance.t())
```

The `TwitterViewer.CreditBalance`. Costs no credits.

# `new`

```elixir
@spec new(keyword()) :: TwitterViewer.Client.t()
```

Builds a client. Raises `ArgumentError` for a missing API key or an invalid option.

Options:

  * `:api_key` - defaults to the `TWITTERVIEWER_API_KEY` environment variable
  * `:base_url` - defaults to `TWITTERVIEWER_BASE_URL`, then `https://twitterviewer.ai`
  * `:timeout` - milliseconds allowed for one HTTP attempt (default `90000`)
  * `:max_retries` - retries after a 429, 5xx (except 501) or network error; `0` disables them
    (default `2`)
  * `:transport` - a 5-arity function, see `TwitterViewer.Transport`
  * `:sleep` - a 1-arity function taking the milliseconds to wait between retries (tests pass
    a fake; default `Process.sleep/1`)

# `replies`

```elixir
@spec replies(TwitterViewer.Client.t(), String.t(), [{:max, pos_integer()}]) ::
  result(TwitterViewer.RepliesResult.t())
```

Reads the replies under a post. `:max` is 1 to 100 (default 20).

# `swap_post`

```elixir
@spec swap_post(
  TwitterViewer.Client.t(),
  String.t(),
  String.t() | pos_integer(),
  options()
) ::
  result(TwitterViewer.Document.t())
```

A post or thread, addressed like on x.com. Same formats as `swap_profile/3`.

# `swap_profile`

```elixir
@spec swap_profile(TwitterViewer.Client.t(), String.t(), options()) ::
  result(TwitterViewer.Document.t())
```

Recent posts of a profile, addressed like on x.com. Returns a `TwitterViewer.Document`,
Markdown unless `:format` is `:obsidian`, `:text`, `:json` or `:pdf`.

# `version`

```elixir
@spec version() :: String.t()
```

The SDK version.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
