TF Fiscal

Authentication & signing

The purpose of the two tokens, and the signature that every request must carry

Two tokens

The open API uses two tokens. Both are signed in the same way, but each gives access to different endpoints:

TokenSourcePurpose
Integrator tokenMain platform, "My Account → Key"; integrator accounts are activated by TFRegistering, editing and listing the issuing companies under the account (/api/company/create, /api/company/v2/create, /api/company/get_list, etc.)
Company tokenThe token field returned by the company registration endpoint; it can also be queried with /api/company/get_listAll other endpoints, on behalf of that company: issuance, queries, cancellation, file downloads, tax category maintenance and so on

A token is equivalent to an account password: it is stored and used only on the server side and must not be included in browser or mobile code. The secret key shown in the main platform is masked; copying the full value requires the sign-in password.

Request headers

HeaderDescription
token *Integrator token or company token, as required by the endpoint
timestamp *Current Unix time in seconds. Requests that differ from the server time by more than 300 seconds are rejected
sign *Signature, 32 lowercase hexadecimal characters
Content-Typeapplication/json, UTF-8 encoded, when there is a request body

Signature algorithm

sign = md5(token + path + body + timestamp)
  • path: the request path, for example /api/invoice/create. GET requests must include the query string, for example /api/invoice/get_list?page=1&page_size=20, with the parameters in the same order as actually sent.
  • body: the raw request body as actually sent, with all carriage returns and line feeds removed before the calculation; an empty string for GET requests. The body must not be altered after signing (including reformatting the JSON).
  • timestamp: exactly the same string as the timestamp header.
  • The concatenated string is encoded in UTF-8 before the MD5 is calculated.

Python

import hashlib, json, time, requests

def chamar(token, path, payload=None):
    body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")) if payload is not None else ""
    ts = str(int(time.time()))
    sign = hashlib.md5((token + path + body.replace("\r", "").replace("\n", "") + ts).encode("utf-8")).hexdigest()
    headers = {"token": token, "timestamp": ts, "sign": sign, "Content-Type": "application/json"}
    url = "https://api.tffiscal.com" + path
    if payload is None:
        return requests.get(url, headers=headers).json()
    return requests.post(url, headers=headers, data=body.encode("utf-8")).json()

Node.js

const crypto = require("crypto");

async function chamar(token, path, payload) {
  const body = payload === undefined ? "" : JSON.stringify(payload);
  const ts = String(Math.floor(Date.now() / 1000));
  const sign = crypto.createHash("md5")
    .update(token + path + body.replace(/[\r\n]/g, "") + ts, "utf8").digest("hex");
  const res = await fetch("https://api.tffiscal.com" + path, {
    method: payload === undefined ? "GET" : "POST",
    headers: { token, timestamp: ts, sign, "Content-Type": "application/json" },
    body: payload === undefined ? undefined : body,
  });
  return res.json();
}

Responses on authentication failure

SituationHTTP statusResponse
Missing sign header400Plain text Missing sign header
Signature mismatch; missing or malformed timestamp; time difference too large401JSON, with code AUTH_SIGN_INVALID, AUTH_TIMESTAMP_MISSING, AUTH_TIMESTAMP_INVALID or AUTH_TIMESTAMP_EXPIRED
Token missing or invalid, or application disabled200success: false, with code AUTH_TOKEN_MISSING, AUTH_TOKEN_INVALID or AUTH_APP_DISABLED
Call rate limit of the token exceeded200success: false, with code RATE_LIMITED
The endpoint requires a subscription, or the subscription balance is insufficient200success: false, with code SUBSCRIPTION_REQUIRED or SUBSCRIPTION_NO_BALANCE
More than 60 requests from the same IP within 10 seconds429JSON {"success":false,"message":"Too many requests, please slow down","code":"RATE_LIMITED"}; this check runs before signature verification

The most common causes of a signature mismatch are: the request body was re-serialized after signing; the path of a GET request omitted the query string; the server clock is inaccurate.

/api/sso/consume_ticket is called by the browser and uses the one-time ticket as its credential; it does not require the three headers above.