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),:postor:conversation. Post links only.:stats-:all(default),:rootor: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.
The TwitterViewer.CreditBalance. Costs no credits.
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
@type options() :: keyword()
@type result(value) :: {:ok, value} | {:error, TwitterViewer.Error.t()}
Functions
@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.
@spec convert_by_query(TwitterViewer.Client.t(), String.t(), options()) :: result(TwitterViewer.ConvertResult.t())
The same conversion through GET /api/v1/convert.
@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.
@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.
@spec credits(TwitterViewer.Client.t()) :: result(TwitterViewer.CreditBalance.t())
The TwitterViewer.CreditBalance. Costs no credits.
@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 theTWITTERVIEWER_API_KEYenvironment variable:base_url- defaults toTWITTERVIEWER_BASE_URL, thenhttps://twitterviewer.ai:timeout- milliseconds allowed for one HTTP attempt (default90000):max_retries- retries after a 429, 5xx (except 501) or network error;0disables them (default2):transport- a 5-arity function, seeTwitterViewer.Transport:sleep- a 1-arity function taking the milliseconds to wait between retries (tests pass a fake; defaultProcess.sleep/1)
@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).
@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.
@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.
@spec version() :: String.t()
The SDK version.