Error Handling

All errors are returned as JSON with a consistent structure. Use the HTTP status code for routing logic and the message field for human-readable details.

Error Response Shape

{
  "error": "Unauthorized",
  "message": "Invalid or missing token"
}

Some 400-series errors include a details field with per-field validation messages.

{
  "error": "Validation failed",
  "message": "Request body is invalid",
  "details": {
    "text": "text is required",
    "secret": "secret must be a non-empty string"
  }
}

HTTP Status Codes

Code Name When it occurs
200 OK Request succeeded. Response body contains the result.
201 Created Resource created (e.g. new API token, new signing channel).
400 Bad Request Invalid JSON, or missing or invalid fields in the request body. Check the details field for per-field messages.
401 Unauthorized Missing, expired, or invalid credential. Re-authenticate and retry.
  • JWT expired → call /api/auth/refresh
  • API token revoked → create a new token
  • No Authorization header present
402 Payment Required Credit balance is zero or insufficient for the requested operation. Top up credits or upgrade your plan.
403 Forbidden Authenticated but not allowed. Causes:
  • Account suspended due to an open dispute
  • Email not verified
  • Token lacks the required permission scope
404 Not Found The requested resource (channel, token, etc.) does not exist or does not belong to your account.
409 Conflict A resource with the same unique identifier already exists (e.g. email already registered, channel name already taken).
413 Payload Too Large The JSON body is over 1 MB. Split longer texts into several calls.
422 Unprocessable Entity A keyed (signing-channel) decode failed: the token belongs to a different channel, or the watermark bits were changed. Carrier-too-short errors are 400, not 422.
429 Too Many Requests Over a rate limit: 600 watermark calls per API token, 100 other requests per IP address, or 100 invalid API tokens per IP address, each within 15 minutes. The body's retryAfter is in minutes; the standard RateLimit response headers show what is left.
500 Internal Server Error Unexpected server-side failure. These are logged automatically. If the issue persists, contact support.

Handling 401 — Token Refresh

JWT access tokens expire after 2 hours. When you receive a 401, call the refresh endpoint to get a new one. The refresh token is stored as a httpOnly cookie and sent automatically by the browser (or --cookie-jar in curl).

POST /api/auth/refresh
Cookie: refresh_token=<cookie-sent-automatically>

# Response
{
  "access_token": "eyJ..."
}

If the refresh token is also expired (30-day TTL), re-authenticate with POST /api/auth/login. A 401 on a watermark endpoint means the API token was revoked, regenerated (the old secret stops working), or has passed its expiry date if one was set.

Handling 402 — Insufficient Credits

Every watermark call (encode, decode, robust, tokens, analyze) costs 1 credit. A 402 means the balance was zero, so the call was not made and nothing was charged. Successful calls return the remaining balance in the X-Credits-Remaining header.

{
  "error": "Insufficient credits",
  "message": "You have no credits remaining. Top up or upgrade your plan.",
  "checkout_url": "https://textwatermarking.com/dashboard#upgrade"
}

Check your balance at GET /api/v2/users/credits (with your login token). Send the user to checkout_url to buy a credit pack or upgrade.

Only successful calls cost a credit. A request refused with 400 or 422 is free, and is checked before the balance, so a bad request returns 400 even when the balance is empty: fix the request first.

Handling 403 — Forbidden

A 403 has one of these bodies:

{ "error": "Account is dispute" }      // a payment dispute froze the account (also "Account is suspended")
{ "error": "Permission denied", "required": ["decode"], "possessed": ["encode"] }   // token lacks the permission
{ "error": "Token limit reached for free plan (max 1)", "details": { ... } }        // creating a token over the plan limit

For a frozen account, contact [email protected]. For permissions, create a token with the permission you need.

Handling 422 — Encode/Decode Failure

Text that is too short returns 400 with required_length (characters needed). 422 is used only when a keyed decode fails. Text without a watermark is not an error: decode returns an empty or null secret.

Message Meaning
400 Visible text too short The carrier has fewer characters than the secret needs (robust and keyed: 8 per payload byte). required_length says how many. Use a longer text or a shorter secret.
422 Decryption failed — wrong channel or corrupted watermark Keyed decode only: the token's signing channel is not the one that encoded the text, or the hidden characters were changed.
200 with integrity.tamper_detected / is_partial Robust decode reports damage in the response instead of failing: is_partial when the text was cut, tamper_detected when bits changed (checksum mismatch).

Client-Side Best Practices

  • Always check the HTTP status code first; do not parse the body unless the status indicates success or a known error shape.
  • On 401: attempt one silent refresh, then redirect to login. Avoid refresh loops.
  • On 429: read the Retry-After header and wait that many seconds before retrying. Do not hammer the endpoint.
  • On 500: retry once with exponential backoff. If errors persist, check the status page or contact support.
  • Do not retry 400, 402, 403, or 422 without first fixing the underlying issue — these are deterministic failures.