For the complete documentation index, see llms.txt. This page is also available as Markdown.

Integrate Clarity

Call the Clarity card and chat from your backend, over the SDK or REST.

Clarity is available to your backend through the TypeScript SDK and a REST API. Both authenticate with a single x-api-key issued in your admin dashboard, and both return the same two response shapes:

  • The card: two short sections (marketContext and positionsAnalysis), follow-up suggestions, citations, and a staleness indicator. Built for portfolio commentary above the fold.

  • The chat: a streamed answer with citations and follow-ups, for a conversational "ask Clarity" surface delivering market, asset, and portfolio insights.

To embed Clarity directly in a browser without building UI, see Embed Clarity. To understand the holdings model these calls run against, see Portfolio context.


Set up the client

import { createClient } from '@motif-ai/sdk'

const motif = createClient({
  apiKey: process.env.MOTIF_API_KEY!,
  // baseURL defaults to https://api.motifapp.ai/api
})

REST calls use the same base URL and key:

POST https://api.motifapp.ai/api/v1/clarity/card
x-api-key: <motif-api-key>
content-type: application/json

Clarity card

Reference a persisted portfolio by id, or pass holdings inline for stateless calls.

Or inline:

Response:

staleness.ageOfNewestDays is how recent Clarity's freshest source is. Render a "data may be stale" badge above your tolerance. narrative carries the structured story-thread view behind the card — see Story threads behind the answer.


Clarity chat

Chat streams the answer so you can render it as it generates.

motif.clarity.chat(...) returns an AsyncGenerator. Iterate it with for await:

The final answer arrives as a CUSTOM event whose name is clarity-answer. Its value carries the structured answer (answerMarkdown, citations, followUps, staleness). Prefer it over the accumulated text chunks. Pass an AbortSignal as the second argument to cancel a stream.

The stream also emits a CUSTOM event named graph-metadata; its value.narrative carries the structured story-thread view behind the answer — stories, what developed, and what changed — for rendering alongside the prose. See Story threads behind the answer.

The response is a text/event-stream of data:-prefixed JSON events. Branch on type, and for CUSTOM also branch on name:

type

Meaning

TEXT_MESSAGE_CONTENT

Streaming text chunk of the answer (delta: string)

CUSTOM

Final answer when name is "clarity-answer"; value has answerMarkdown, citations, followUps, staleness

CUSTOM

Story-thread view when name is "graph-metadata"; value.narrative carries the stories, developments, and state changes behind the answer

TOOL_CALL_START / TOOL_CALL_ARGS / TOOL_CALL_END / TOOL_CALL_RESULT

Internal tool traces, safe to ignore for display

RUN_ERROR

An error occurred

The stream terminates when the connection closes. A RUN_FINISHED event may precede the close.

threadId is optional but recommended. It lets Clarity correlate turns within a conversation. Supply portfolio inline instead of portfolioId for stateless callers.


Next steps

Last updated