0

Search

Published today

Search the web with Linkup and get ranked sources, a sourced answer or structured JSON matching a schema.

Script linkup Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * Search
5
 * Search the web with Linkup and get ranked sources, a sourced answer or structured JSON matching a schema.
6
 */
7
export async function main(
8
  auth: RT.Linkup,
9
  query: string,
10
  depth: "flash" | "fast" | "standard" | "deep",
11
  output_type: "searchResults" | "sourcedAnswer" | "structured",
12
  structured_output_schema: { [key: string]: any } | undefined,
13
  include_sources: boolean | undefined,
14
  include_inline_citations: boolean | undefined,
15
  include_images: boolean | undefined,
16
  include_domains: string[] | undefined,
17
  exclude_domains: string[] | undefined,
18
  from_date: string | undefined,
19
  to_date: string | undefined,
20
  max_results: number | undefined
21
) {
22
  const url = new URL("https://api.linkup.so/v1/search")
23

24
  const hasSchema =
25
    structured_output_schema != null &&
26
    Object.keys(structured_output_schema).length > 0
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      Authorization: `Bearer ${auth.token}`,
32
      "Content-Type": "application/json",
33
      Accept: "application/json",
34
    },
35
    body: JSON.stringify({
36
      q: query,
37
      depth: depth || "fast",
38
      outputType: output_type || "searchResults",
39
      structuredOutputSchema: hasSchema
40
        ? JSON.stringify(structured_output_schema)
41
        : undefined,
42
      includeSources: include_sources || undefined,
43
      includeInlineCitations: include_inline_citations || undefined,
44
      includeImages: include_images || undefined,
45
      includeDomains: include_domains?.length ? include_domains : undefined,
46
      excludeDomains: exclude_domains?.length ? exclude_domains : undefined,
47
      fromDate: from_date || undefined,
48
      toDate: to_date || undefined,
49
      maxResults: max_results ?? undefined,
50
    }),
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  return await response.json()
58
}
59