Skip to main content

API libraries

You can call the Ha Tang REST API from any language with a plain HTTP client. This page lists the official libraries that make it easier, and shows how to generate a typed client from the OpenAPI spec for languages without an official SDK.

Node.js: @hatang/sdk​

The official server SDK covers Email, Support, Products, CRM, Payments and e-invoices, with TypeScript types for every request and response.

npm install @hatang/sdk
import { Hatang } from "@hatang/sdk";

const hatang = new Hatang(process.env.HATANG_API_KEY, {
baseUrl: "https://api.hatang.dev",
// environment: "development", // Payments only; sent as Hatang-Environment
});

const { data, error } = await hatang.emails.send({
from: "Shop <[email protected]>",
subject: "Welcome",
html: "<p>Welcome to the shop!</p>",
});

What to know:

  • Results, not exceptions. Every method resolves to { data, error } and never throws for API errors. error has the usual statusCode, name and message (Errors). A network failure gives statusCode: 0.
  • Configuration. Pass the key and options to the constructor, or set HATANG_API_KEY, HATANG_BASE_URL and HATANG_ENVIRONMENT. Always set the base URL to https://api.hatang.dev.
  • Namespaces follow the API: emails, batch, domains, templates, contacts, contactProperties, audiences, segments, topics, broadcasts, automations, events, apiKeys, webhooks, logs, usage, chat, payments and einvoices.
  • Idempotency. Methods that create things take an { idempotencyKey } option (Idempotency).
  • Webhook helpers. verifyWebhook and constructWebhookEvent check webhook signatures; verifyIpn and constructIpnEvent check Payments IPN calls (Webhooks).
  • React Email. Pass a react element to emails.send and the SDK renders it to HTML and plain text (install @react-email/render and react alongside).

The Node.js SDK guide covers every namespace with examples.

Chat SDKs for web and mobile​

The chat SDKs run in your visitors' browsers and apps. They connect to the chat gateway with a public site id (and an app key on mobile), never with an API key.

PlatformPackage
Websitehttps://chat.hatang.dev/widget.js
React / Next.js@hatang/chat-react
WordPressHa Tang Chat plugin
Flutterhatang_chat
React Native / Expo@hatang/chat-react-native
iOS (Swift)HatangChat
Android (Kotlin)dev.hatang:chat

The SDK overview links the guide for each, the supported versions and the realtime protocol they share.

Other languages​

There is no official server SDK for PHP, Python or Go yet. Two good options:

  • Call the REST API directly. Every guide in this hub has cURL, Python and PHP samples. The API is plain JSON over HTTPS with a Bearer token.
  • Generate a client from the OpenAPI spec, as below.

The OpenAPI spec​

The REST API is described by an OpenAPI 3.1 document, generated from the same code that serves the API:

SpecURL
REST APIhttps://api.hatang.dev/openapi.json
Chat gatewayhttps://chat.hatang.dev/openapi.json

Browse it in the REST API reference and the chat gateway reference, or import it into an API client such as Postman or Insomnia to try calls by hand.

Generate a client​

Point a generator at the spec URL. Some examples:

# TypeScript types (use them with openapi-fetch or your own fetch wrapper)
npx openapi-typescript https://api.hatang.dev/openapi.json -o hatang.d.ts

# Python
pipx run openapi-python-client generate --url https://api.hatang.dev/openapi.json

# Many languages, including PHP, Java, Kotlin, C# and Ruby
npx @openapitools/openapi-generator-cli generate \
-i https://api.hatang.dev/openapi.json -g php -o ./hatang-php
note

The spec uses OpenAPI 3.1. Check that your generator supports 3.1; some only handle 3.0 fully. Regenerate the client when you upgrade, as new endpoints and fields are added to the spec as they ship.

Generated clients send what you give them, so remember the shared rules: the Authorization: Bearer header, Idempotency-Key on sends and payments, pagination with after, and retries on 429 (Rate limits).

Next steps​

View this page as Markdown