Forked from tluyben/gist:1963331b906568cda67c4814b8ed8311
Last active
July 20, 2025 07:38
-
-
Save fry69/983fe714cc6bd72650c5726ac7df5806 to your computer and use it in GitHub Desktop.
Get free models from OpenRouter as JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S deno run --allow-net | |
| /** | |
| * Fetches all models from OpenRouter API and filters for free ones | |
| * Run with: deno run --allow-net openrouter-free-models.js | |
| */ | |
| // Define interfaces for OpenRouter API response types | |
| interface ModelPricing { | |
| prompt: string; | |
| completion: string; | |
| } | |
| interface OpenRouterModel { | |
| id: string; | |
| name?: string; | |
| description?: string; | |
| context_length?: number; | |
| pricing?: ModelPricing; | |
| } | |
| interface OpenRouterResponse { | |
| data: OpenRouterModel[]; | |
| } | |
| async function getFreeOpenRouterModels(): Promise<OpenRouterModel[]> { | |
| try { | |
| console.log('Fetching models from OpenRouter...'); | |
| const response = await fetch('https://openrouter.ai/api/v1/models', { | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| } | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`HTTP error! status: ${response.status}`); | |
| } | |
| const data: OpenRouterResponse = await response.json(); | |
| if (!data.data || !Array.isArray(data.data)) { | |
| throw new Error('Unexpected API response format'); | |
| } | |
| console.log(`Total models found: ${data.data.length}`); | |
| // Filter for free models | |
| // Free models typically have pricing.prompt = "0" and pricing.completion = "0" | |
| const freeModels = data.data.filter((model: OpenRouterModel) => { | |
| const pricing = model.pricing; | |
| if (!pricing) return false; | |
| // Check if both prompt and completion pricing are "0" (free) | |
| const promptPrice = parseFloat(pricing.prompt || "0"); | |
| const completionPrice = parseFloat(pricing.completion || "0"); | |
| return promptPrice === 0 && completionPrice === 0; | |
| }); | |
| console.log(`Free models found: ${freeModels.length}\n`); | |
| // Display the free models | |
| if (freeModels.length > 0) { | |
| console.log('📋 Free Models Available:'); | |
| console.log('=' .repeat(50)); | |
| freeModels.forEach((model: OpenRouterModel, index: number) => { | |
| console.log(`${index + 1}. ${model.id}`); | |
| if (model.name && model.name !== model.id) { | |
| console.log(` Name: ${model.name}`); | |
| } | |
| if (model.description) { | |
| console.log(` Description: ${model.description}`); | |
| } | |
| if (model.context_length) { | |
| console.log(` Context Length: ${model.context_length.toLocaleString()}`); | |
| } | |
| console.log(''); | |
| }); | |
| } else { | |
| console.log('No free models found.'); | |
| } | |
| // Return the array for potential programmatic use | |
| return freeModels; | |
| } catch (error) { | |
| const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; | |
| console.error('Error fetching models:', errorMessage); | |
| return []; | |
| } | |
| } | |
| // Run the function if this script is executed directly | |
| if (import.meta.main) { | |
| const freeModels = await getFreeOpenRouterModels(); | |
| // Optional: Save to JSON file | |
| const saveToFile = Deno.args.includes('--save'); | |
| if (saveToFile) { | |
| try { | |
| await Deno.writeTextFile( | |
| 'openrouter-free-models.json', | |
| JSON.stringify(freeModels, null, 2) | |
| ); | |
| console.log('✅ Results saved to openrouter-free-models.json'); | |
| } catch (error) { | |
| const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; | |
| console.error('Failed to save file:', errorMessage); | |
| } | |
| } | |
| } | |
| // Export for use as a module | |
| export { getFreeOpenRouterModels }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment