@hautechai/sdk
v2.41.0
Published
Hautech SDK
Readme
Hautech SDK
Installation
npm install @hautechai/sdk
# or
yarn add @hautechai/sdkUsage
Creating tokens
To use the SDK, first you need to create a token via token signer.
// THIS CODE SHOULD BE RUN ON SERVER SIDE ONLY!!!
import { createTokenSigner } from '@hautechai/sdk';
const signer = createTokenSigner({
appId: process.env.APP_ID!,
appKeyId: process.env.APP_KEY_ID!,
appKeySecret: process.env.APP_KEY_SECRET!,
});Root token
For creating token that can be used on server side, you can create a root token.
const rootToken = signer.createRootToken({ expiresInSeconds: 3600 });It's very not recommended to use root token on client side.
Account token
For creating token that can be used on client side by your users, you can create an account token.
const accountToken = signer.createAccountToken({ accountId: 'ACCOUNT_ID', expiresInSeconds: 3600 });Initializing SDK
To initialize the SDK, you need to pass the function that returns the token to the createSDK function.
import { createSDK } from '@hautechai/sdk';
const sdk = createSDK({ authToken: () => accountToken }); // you should call the server here for getting the tokenHandling request errors and retries
You can centralize HTTP error handling by providing an onRequestError hook. The hook receives the typed
RequestErrorInfo and RequestContextInfo, and can instruct the SDK to retry the request, invalidate the cached
token, and optionally wait before retrying.
const sdk = createSDK({
baseUrl,
authToken: getCurrentToken,
onRequestError: async ({ error }) => {
if (error.status === 401) {
await refreshToken();
return { retry: true, invalidateToken: true };
}
return { retry: false };
},
});
// Later, when using websockets, you can disconnect explicitly
sdk.ws.disconnect();Using SDK
Docs about how to use the SDK are available here
Uploading files
- In browsers, prefer passing a
Fileobject to methods likesdk.images.createFromFileandsdk.videos.createFromFile. TheFile.namewill be included in the multipart upload automatically. - If you pass a
Blobin the browser, some environments may set the filename to a default value (e.g.,"blob"). To control the filename when using aBlob, wrap it in an object with explicit metadata on server-side, or construct aFilefrom theBlob. - In Node.js, when you pass a string path (e.g.,
/path/to/image.png), the SDK sets the multipart filename to the basename of the path (e.g.,image.png). You can also pass an object with{ stream, filename, contentType }to control metadata explicitly.
Development
Prerequisites
- Node.js 22+
- PNPM 10+
Installation
pnpm install --frozen-lockfileBuilding
pnpm buildTesting
# Run all tests (unit + e2e)
pnpm test
# Run unit tests only
pnpm test:unit
# Run e2e tests only
pnpm test:e2e
# Run tests with coverage
pnpm test:covLinting and Formatting
# Check code formatting
npx prettier --check .
# Fix code formatting
npx prettier --write .
# Type checking
npx tsc --noEmit