SDKs
Official client libraries for TypeScript and Python. Each provides one method per endpoint, typed errors, and a retry policy that never resends a request that may have been charged.
Version 0.2.0 of both packages supports all API features except freezing. To send freeze_citations, call the REST API directly. <freeze> tags are part of text and are supported by the SDKs.
Calling the API#
Create a single client instance and reuse it. Both clients take the API key as an argument. The samples read it from an environment variable.
import { WriteHuman } from "@writehuman/sdk"; const wh = new WriteHuman({ apiKey: process.env.WRITEHUMAN_API_KEY! }); // Humanize text. All options are optional.const out = await wh.humanize(text, { tone: "professional", language: "English" });console.log(out.results[0]); // Score text for AI authorship.const scan = await wh.detect(text, { includeSentences: true });console.log(scan.ai_probability, scan.predicted_class); // Retrieve usage. This call is not billed.const usage = await wh.usage();console.log(usage.topup_balance);Responses contain the same fields as the REST API: see humanize, detect and usage. In TypeScript, responses are typed objects. In Python, responses are dictionaries, and the package exports type definitions for static type checkers.
Async Python#
AsyncWriteHuman provides the same three methods as coroutines.
import osfrom writehuman import AsyncWriteHuman async with AsyncWriteHuman(os.environ["WRITEHUMAN_API_KEY"]) as wh: out = await wh.humanize(text)Errors#
In the official SDKs every failure is a subclass of WriteHumanError carrying code, status and the last Retry-After (retry_after in Python, retryAfter in TypeScript). InsufficientBalanceError adds words_remaining / wordsRemaining, and PlanRequiredError adds upgrade_url / upgradeUrl. A code the installed SDK version does not know becomes APIError with code preserved. A response that is not the API's JSON, no response within the client timeout, or a failed connection becomes ProtocolError, ClientTimeoutError or ConnectionError, with no code.
import { InsufficientBalanceError, RateLimitError } from "@writehuman/sdk"; try { await wh.humanize(text);} catch (e) { if (e instanceof InsufficientBalanceError) { console.log("out of words:", e.wordsRemaining); } else if (e instanceof RateLimitError) { console.log("retry in", e.retryAfter, "seconds"); } else { throw e; }}The Errors page lists the error class for each code.
Retries#
A 429, 503 or 504 response that includes the API's error body is retried after the interval given in Retry-After, up to a maximum of 2 retries. If Retry-After exceeds 60 s, as with an authentication lockout, the SDK does not wait and raises the error immediately, with the interval available as retry_after (Python) or retryAfter (TypeScript). A humanize or detect request that may have been charged is never resent.
No other failures are retried. On humanize and detect, a client timeout or a dropped connection raises an error immediately, because the request may have completed and been charged. usage is read-only and not billed, so it is also retried in these cases.
Timeouts#
A timeout is a deadline for the entire response, not a per-read limit. The defaults are 120 seconds for humanize, 30 for detect and 15 for usage. Timeouts can be set for the client, per operation or per call.
const wh = new WriteHuman({ apiKey: process.env.WRITEHUMAN_API_KEY!, timeoutMs: { humanize: 180_000 }, maxRetries: 3,}); // Or for one call:await wh.detect(text, { timeoutMs: 10_000 });The API responds to every humanize request within 170 seconds. With the default 120-second timeout, a slow request can complete, and be charged, after the client has timed out. For long documents, increase the timeout to 180 seconds. See Production best practices.