TwitterViewer for Elixir

Official Elixir client for the TwitterViewer API: read public X (Twitter) posts, threads, profiles and replies as JSON, Markdown, text or PDF. All access is read-only. TwitterViewer is an independent service, not the official X API.

Requires Elixir 1.18 or later. No runtime dependencies (:httpc and the built-in JSON).

Installation

def deps do
  [{:twitterviewer, "~> 0.1"}]
end

Quickstart

The API needs a Plus plan: every call from an account without it fails with 402 LIMIT_REACHED and details["reason"] = "PLUS_REQUIRED" (API, MCP and CLI access are all part of Plus). Create an API key at https://twitterviewer.ai/account/api and export it as TWITTERVIEWER_API_KEY.

client = TwitterViewer.new() # or TwitterViewer.new(api_key: "tv_live_...")

{:ok, thread} = TwitterViewer.convert(client, "https://x.com/jack/status/20")

IO.puts(thread.markdown)
IO.puts("charged #{thread.credits.charged}, #{thread.credits.remaining} left")

Configuration

Option Default Meaning
:api_key TWITTERVIEWER_API_KEY Required.
:base_url TWITTERVIEWER_BASE_URL, then https://twitterviewer.ai
:timeout 90_000 Milliseconds allowed for one HTTP attempt.
:max_retries 2 Retries after a transient failure; see Errors and retries. 0 disables them.
:transport :httpc A 5-arity function, see TwitterViewer.Transport. Use it for another HTTP client or a proxy.
:sleep Process.sleep/1 Called with the milliseconds to wait between retries.

TwitterViewer.new/1 raises ArgumentError when the key is missing or an option is invalid. A client is a plain value: share it between processes.

Functions

Every function takes the client first and returns {:ok, value} or {:error, error}. Responses are structs with snake_case fields (post.created_at is a DateTime, post.reply_count, and so on).

Convert

The argument is a link to an X post or profile, or an @handle. Profile links return recent posts.

# Structured result (POST /api/v1/convert)
{:ok, result} = TwitterViewer.convert(client, "https://x.com/jack/status/20", scope: :conversation)
for post <- result.posts, do: IO.puts("#{post.author.handle}: #{post.text}")

# The same through GET /api/v1/convert
{:ok, profile} = TwitterViewer.convert_by_query(client, "@NASA", max: 50, reposts: true)

# Only the document body: :markdown (default), :obsidian or :text
{:ok, document} = TwitterViewer.convert_to_text(client, "@NASA", format: :obsidian, stats: :off)
:ok = TwitterViewer.Document.save(document, "nasa.md")

# PDF, rendered by the server
{:ok, pdf} = TwitterViewer.convert_to_pdf(client, "https://x.com/jack/status/20", paper: :letter)
:ok = TwitterViewer.Document.save(pdf, pdf.filename || "thread.pdf")
Option Values Server default
:scope :thread, :post, :conversation (post links) :thread
:format depends on the function
:stats :all, :root, :off :all
:max 1 to 200 (profile links) 20
:replies include replies (profile links) false
:reposts include reposts (profile links) false
:paper :a4, :letter (PDF) :a4

Options you leave out are not sent. Atoms and strings are both accepted for the values.

A TwitterViewer.Document has body, content_type, filename (PDF), credits_charged, credits_remaining, rate_limit_remaining and posts_returned (Markdown and text).

Replies

{:ok, result} = TwitterViewer.replies(client, "https://x.com/jack/status/20", max: 50)
for reply <- result.replies, do: IO.puts(reply.text)

Credits

{:ok, balance} = TwitterViewer.credits(client) # costs no credits
IO.inspect({balance.total, balance.plan.remaining, balance.purchased.remaining})

Domain swap

The same conversion addressed like an x.com URL. Returns a Markdown Document unless :format says otherwise.

{:ok, %{body: markdown}} = TwitterViewer.swap_profile(client, "NASA", max: 10)
{:ok, %{body: text}} = TwitterViewer.swap_post(client, "jack", "20", format: :text)
{:ok, document} = TwitterViewer.swap_post(client, "jack", "20", format: :json)
{:ok, data} = TwitterViewer.Document.to_convert_result(document)

Errors and retries

Every error a call returns is an exception struct, so raise error works. TwitterViewer.Error.t() is their union.

Struct When
TwitterViewer.APIError Non-2xx answer. Has status, code, retry_after_sec, details (string keys).
TwitterViewer.ConnectionError No response (DNS, connection, TLS). reason is what the transport reported.
TwitterViewer.TimeoutError No response: the attempt ran out of time. TwitterViewer.Error.connection?/1 is true for it and for ConnectionError.
TwitterViewer.UnexpectedResponseError A 2xx body that cannot be read, usually a wrong base URL.

Values that can never work raise ArgumentError, and nothing is sent.

case TwitterViewer.replies(client, "https://x.com/jack/status/20") do
  {:ok, result} ->
    result

  {:error, %TwitterViewer.APIError{code: "LIMIT_REACHED", details: details}} ->
    IO.puts(details["reason"]) # PLUS_REQUIRED or INSUFFICIENT_CREDITS

  {:error, error} ->
    raise error
end

TwitterViewer.ErrorCode lists every code the service defines (ErrorCode.limit_reached/0, ErrorCode.all/0).

Every function is retried the same way, up to :max_retries times:

How retrying behaves:

Retrying does not cost credits twice: see Credits.

Credits

1 credit = 1 post returned. A quoted post counts as one more post; author profiles are free. Errors and empty results are free.

A repeated request by the same account within 10 minutes returns the first result and is not charged again, so retrying is safe. The same request means:

If that result was partial (limited by the balance, or a free website preview of a conversion) and the account now has credits, the request is treated as new and charged. Every authenticated request, including credits, counts toward the per-minute rate limit. Full rules: https://twitterviewer.ai/docs/api.

Development

mix deps.get
mix format --check-formatted
mix compile --warnings-as-errors
mix test
mix docs
mix hex.build

Integration tests are skipped unless both variables are set (they spend a few credits):

TWITTERVIEWER_BASE_URL=http://localhost:3400 TWITTERVIEWER_API_KEY=tv_live_... mix test

openapi.json is the contract; a test fails when an operation has no function.

License

MIT