TypeScript SDK
@typemo-org/sdk is a typed client for both Typemo APIs. Search, suggestions and events go to the
search API; everything else goes to the management API. One object covers both, and its types come
from the same OpenAPI document as the API reference.
npm i @typemo-org/sdkpnpm add @typemo-org/sdkyarn add @typemo-org/sdkbun add @typemo-org/sdkimport { Typemo } from '@typemo-org/sdk';
const typemo = new Typemo({ apiKey: process.env.TYPEMO_API_KEY });It runs on Node 18 or later and in browsers. In a browser, use a Read only key and set searchUrl to
your region’s address: see Regions.
Options
Section titled “Options”| Option | What it does | Default |
|---|---|---|
apiKey | The key, sent as X-API-Key. | None |
baseUrl | The management API. | https://api.typemo.com/v1 |
searchUrl | The search API: search, suggestions and events. In a browser, your region’s address plus /v1. | https://search.typemo.com/v1 |
fetch | A fetch of your own, such as SvelteKit’s event.fetch. | The global fetch |
credentials | Passed to fetch. include sends cookies. | None |
Search
Section titled “Search”interface Product { title: string; price: number; brand: string;}
const results = await typemo.search<Product>('products', { q: 'sneakers', filter: { AND: [ { field: 'brand', op: 'eq', value: 'Stride' }, { field: 'price', op: 'lte', value: 100 } ] }, sort: 'price:asc', perPage: 20});
results.found; // total matchesresults.hits[0]?.title; // your document fields, typed, plus id and _scoreresults.facets.category; // [['Footwear', 3]]results.fallback?.hits; // on a miss: popular items to show insteadThe type parameter types the hits’ fields. Every parameter is optional and matches the
Search API one to one. A search goes out as GET, so a repeat
is answered from the one-minute cache; a search with a vector goes out as POST.
Methods
Section titled “Methods”Collection ids and project ids are the first argument. Lists take { page, perPage } and return
{ data, page, perPage, total }.
| Method | What it does |
|---|---|
search(collectionId, params) | Searches a collection. |
suggestions(collectionId, { q, limit, locale }) | Popular searches, optionally those that match q. |
events.track(collectionId, event) | Records a search, click, add-to-cart or purchase. See Analytics events. |
collections.list · get · create · update · delete | Manages collections. |
collections.sync(collectionId) | Re-reads the collection’s source now: a feed, a store or a crawl. |
documents.list(collectionId, query) | Lists documents. Takes q, filter, sort and grouped besides paging. |
documents.get · delete | Reads or deletes one document by id. |
documents.upsert(collectionId, { documents }) | Creates or updates documents by id. Takes any number and sends them 1,000 per request; the counts add up. |
documents.deleteMany(collectionId, ids) | Deletes up to 1,000 documents. Ids that don’t exist count in notFound. |
documents.truncate(collectionId) | Deletes every document. |
curations.list · get · create · update · delete | Manages custom results. |
boostRules.list · get · create · update · delete | Manages boost rules. |
synonyms.list · get · upsert · delete | Manages synonyms. upsert creates or replaces the set with that id. |
synonyms.batch(collectionId, input) | Creates or replaces up to 500 synonym sets in one request. |
stopwords.get · update | Reads or replaces the collection’s stopword list. |
knowledge.list · create · update · delete | Manages the AI assistant’s knowledge snippets, per project. |
analytics.topQueries · zeroResults · topClicked | Reads search analytics, per project. from and to are required. |
raw | The underlying openapi-fetch client, typed for every route. |
Writes need a Write & Read key, so keep that key on your server. Document limits are the same as on every import path: see Formats and limits.
The SDK has no method to replace every document at once. Send that snapshot with
PUT /v1/collections/{id}/documents, described in
Push over the API.
Errors
Section titled “Errors”Every method throws a TypemoError on a response that isn’t 2xx.
| Property | What it holds |
|---|---|
status | The HTTP status. |
code | The error code, such as BAD_REQUEST or RATE_LIMITED. |
message | What went wrong. |
param | The input the API refused, when it names one. |
body | The parsed response. A refused push carries its per-document report here. |
retryAfter | On a 429, the seconds to wait. |
rateLimit | { limit, remaining, reset } from the response headers, each only when the API sent it. |
import { TypemoError } from '@typemo-org/sdk';
try { await typemo.search('products', { q: 'sneakers', sort: 'colour:asc' });} catch (err) { if (!(err instanceof TypemoError)) throw err; console.log(err.status, err.code, err.param); // 400 'BAD_REQUEST' 'sort'}Next: Search API for every search parameter, or Push over the API to keep your catalog current.