CI HUBCI HUB SDK
Guides

Search assets

Run a keyword search, narrow it with the facets a DAM exposes, and page through the results.

This guide runs a keyword search against the connected DAM, narrows it with a facet the provider exposes, and pages through the matches. It assumes you completed the authentication flow: a seeded TokenManager on the client-library path, or a CI HUB access token plus a DAM connection token on the HTTP path.

import { CiHubAccessClient, TokenManager, type AssetSearchOptions } from '@ci-hub/access-sdk'

const client = new CiHubAccessClient({ baseUrl: 'https://stage.ci-hub.com/api/v1' })
const tokens = new TokenManager(client) // seeded during authentication

const search = (options: AssetSearchOptions) =>
  tokens.withDamAuth('dropbox', (accessToken, damToken) =>
    client.search({ accessToken, damToken, options }),
  )
const BASE_URL = 'https://stage.ci-hub.com/api/v1'

const authHeaders = {
  Authorization: `Bearer ${ciHubAccessToken}`,
  'provider-authorization': `Bearer ${damProviderToken}`,
}

async function search(params: Record<string, string | string[]>) {
  const url = new URL(`${BASE_URL}/assets/search`)
  for (const [key, value] of Object.entries(params)) {
    for (const v of Array.isArray(value) ? value : [value]) {
      url.searchParams.append(key, v)
    }
  }

  const response = await fetch(url, { headers: authHeaders })
  if (!response.ok) {
    const { error } = await response.json()
    throw new Error(`${error.code}: ${error.message}`)
  }
  return response.json()
}

Both variants give you the same search(...) used below; the client library additionally refreshes expired tokens and throws a typed AccessSdkError.

Search by keyword

Search takes a query term and returns a page of assets plus the facets this connection exposes. Which fields a keyword matches is defined by the DAM.

const first = await search({ query: 'logo' })
console.log(`${first.totalAssetsCount} matches`)

Narrow with a facet

The filters array lists the facets the DAM offers. Each filter has an id and a set of options, and each option has its own id. Collect the option ids you want and send them as the filters query parameter; repeat it to combine selections. The same facets appear in Provider info, so you can render the controls before the first search.

for (const filter of first.filters ?? []) {
  console.log(filter.name, filter.options.map((o) => o.id))
}

// Send the chosen option ids as the filters parameter.
const narrowed = await search({ query: 'logo', filters: ['file_type:jpg'] })

Page through the results

A response carries more while assets remain. Send it back unchanged to get the next page, and repeat the same query and facet parameters on every page. Stop when more is absent. See Pagination.

const params = { query: 'logo', filters: ['file_type:jpg'] }
const assets = [...narrowed.assets]

let more = narrowed.more
while (more !== undefined) {
  const page = await search({ ...params, more: String(more) })
  assets.push(...page.assets)
  more = page.more
}

Each result asset carries its own thumbnailUrl and downloadUrl. See Asset model for the fields and Download an asset to fetch one.

Next

On this page