Created
July 19, 2025 09:49
-
-
Save tluyben/1963331b906568cda67c4814b8ed8311 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 | |
*/ | |
async function getFreeOpenRouterModels() { | |
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 = 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 => { | |
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, index) => { | |
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) { | |
console.error('Error fetching models:', error.message); | |
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) { | |
console.error('Failed to save file:', error.message); | |
} | |
} | |
} | |
// 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