TwitterViewer (TwitterViewer v0.1.0)

Copy Markdown View Source

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.

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.

Summary

Functions

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

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

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

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

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

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

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

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

The SDK version.

Types

options()

@type options() :: keyword()

result(value)

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

Functions

convert(client, url, opts \\ [])

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(client, url, opts \\ [])

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

convert_to_pdf(client, url, opts \\ [])

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

convert_to_text(client, url, opts \\ [])

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

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

credits(client)

The TwitterViewer.CreditBalance. Costs no credits.

new(opts \\ [])

@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(client, url, opts \\ [])

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

swap_post(client, handle, post_id, opts \\ [])

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

swap_profile(client, handle, opts \\ [])

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

version()

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

The SDK version.