Open History to see the last commit
1/**
2 * Fetches models from the OpenRouter API and writes them to openrouter.models.json.
3 *
4 * Usage:
5 * pnpm tsx scripts/fetch-openrouter-models.ts
6 *
7 * The output is plain JSON so a malicious or compromised upstream response
8 * cannot smuggle executable code into the build (JSON.stringify cannot produce
9 * a JS expression). The committed wrapper at `openrouter.models.ts` re-exports
10 * this JSON typed as `Array<OpenRouterModel>` so consumers don't need to know
11 * where the data lives.
12 */
13
14import { writeFile } from 'node:fs/promises'
15import { dirname, resolve } from 'node:path'
16import { fileURLToPath } from 'node:url'
17
18const __dirname = dirname(fileURLToPath(import.meta.url))
19const OUTPUT_PATH = resolve(__dirname, 'openrouter.models.json')
20const API_URL = 'https://openrouter.ai/api/v1/models'
21
22interface ApiModel {
23 id: string
24 canonical_slug?: string
25 hugging_face_id?: string | null
26 name: string
27 created?: number
28 description?: string
29 context_length: number
30 architecture: {
31 modality: string
32 input_modalities: Array<string>
33 output_modalities: Array<string>
34 tokenizer?: string
35 instruct_type?: string | null
36 } | null
37 pricing: {
38 prompt: string
39 completion: string
40 audio?: string
41 request?: string
42 image?: string
43 web_search?: string
44 internal_reasoning?: string
45 input_cache_read?: string
46 input_cache_write?: string
47 } | null
48 top_provider: {
49 context_length: number
50 max_completion_tokens: number | null
51 is_moderated: boolean
52 } | null
53 per_request_limits?: Record<string, string> | null
54 supported_parameters?: Array<string>
55}
56
57function isValidModel(model: ApiModel): boolean {
58 if (
59 typeof model.id !== 'string' ||
60 typeof model.name !== 'string' ||
61 typeof model.context_length !== 'number' ||
62 model.architecture == null ||
63 model.pricing == null ||
64 model.top_provider == null
65 ) {
66 return false
67 }
68 if (model.created !== undefined && !Number.isFinite(model.created)) {
69 return false
70 }
71 const tp = model.top_provider
72 if (!Number.isFinite(tp.context_length)) return false
73 if (
74 tp.max_completion_tokens !== null &&
75 !Number.isFinite(tp.max_completion_tokens)
76 ) {
77 return false
78 }
79 if (typeof tp.is_moderated !== 'boolean') return false
80 return true
81}
82
83async function main() {
84 console.log(`Fetching models from ${API_URL}...`)
85 const response = await fetch(API_URL, {
86 signal: AbortSignal.timeout(30_000),
87 })
88
89 if (!response.ok) {
90 throw new Error(
91 `Failed to fetch models: ${response.status} ${response.statusText}`,
92 )
93 }
94
95 const json = (await response.json()) as { data: Array<ApiModel> }
96 const allModels = json.data
97
98 const validModels = allModels.filter(isValidModel)
99 const skipped = allModels.length - validModels.length
100 if (skipped > 0) {
101 console.log(
102 `Skipped ${skipped} models missing required fields (id, name, context_length, architecture, pricing, top_provider)`,
103 )
104 }
105
106 validModels.sort((a, b) => a.id.localeCompare(b.id))
107
108 await writeFile(
109 OUTPUT_PATH,
110 JSON.stringify(validModels, null, 2) + '\n',
111 'utf-8',
112 )
113 console.log(`Fetched ${validModels.length} models`)
114 console.log(`Written to ${OUTPUT_PATH}`)
115}
116
117main().catch((error) => {
118 console.error(error)
119 process.exit(1)
120})