You need to install `typesense` to get the types right.
npm install typesense --save-dev
And then create the instance of the client
const API_KEY = "ryl_sk_YOUR_SECRET_KEY";
const client = new SearchClient({ apiKey: API_KEY });
const searchResult = await client.searchAll("Alice");
} from "typesense/lib/Typesense/Documents";
export class SearchClient {
private RAW_RESPONSES: boolean;
static DEFAULT_API_PATH = "/v0/provenance/search";
host = `https://api.royal.io/${SearchClient.DEFAULT_API_PATH}`,
this.RAW_RESPONSES = rawResponses;
public async performSearch<T extends {}>(
searchParameters: SearchParams | SearchParamsWithPreset,
options: SearchOptions = {}
): Promise<PerformSearchResponse<T>> {
const rawResults = await fetch(this.HOST, {
"Content-Type": "application/json",
...(this.API_KEY ? { Authorization: `Bearer ${this.API_KEY}` } : {}),
rawResults.status !== 200 ||
rawResults.headers.get("Content-Type")?.startsWith("text/")
const errorResult = await rawResults.text();
throw new Error(errorResult);
const results = await rawResults.json();
if (this.RAW_RESPONSES) {
return results as PerformSearchResponse<T>;
data: ((results as SearchResponse<T>).hits || []).map(
(hit) => hit.document as T
total: (results as SearchResponse<T>).found,
numPages: Math.ceil((results as SearchResponse<T>).found / PAGE_SIZE),
console.error("Search failed: ", e);
public searchAll(q = "", page = 1, perPage = PAGE_SIZE) {
return this.performSearch<
AccountDocumentType | ProvenanceClaimDocumentType
query_by: "username,psqlId,contentHash,metadata.name,custody",
public searchAccounts(q = "", page = 1, perPage = PAGE_SIZE) {
return this.performSearch<AccountDocumentType>({
query_by: "username,psqlId,custody",
filter_by: "type:account",
public searchClaims(q = "", page = 1, perPage = PAGE_SIZE) {
return this.performSearch<ProvenanceClaimDocumentType>({
query_by: "metadata.name,originatorUsername,psqlId,contentHash",
filter_by: "type:provenanceClaim",
type AccountDocumentType = {
provenanceClaimCount: number;
originatedProvenanceClaimCount: number;
registeredProvenanceClaimCount: number;
type ProvenanceClaimDocumentType = {
originatorAddress: string;
originatorUsername: string;
registrarAddress: string;
registrarUsername: string;
transactionIndex: number;
metadata?: PCMetadataType;
type PCMetadataType = Record<string, any> & {
type PerformSearchResponse<T> = {