Fichier fonctionnel d’origine
reference/list-text-models.mjs
Le code et les pages d’exemple fonctionnelles conservent leurs identifiants et leurs textes d’origine. La leçon et les consignes des fichiers les expliquent dans votre langue.
// Private command-line helper: never copy this file or .env into public/.
const key = process.env.GEMINI_API_KEY;
if (!key) throw Error("Set GEMINI_API_KEY in the private root environment first.");
let token = "",
pages = 0;
const names = new Set();
do {
const url = new URL("https://generativelanguage.googleapis.com/v1beta/models");
if (token) url.searchParams.set("pageToken", token);
const r = await fetch(url, {
headers: { "x-goog-api-key": key },
signal: AbortSignal.timeout(10000),
});
if (!r.ok)
throw Error(
"Model list request failed (HTTP " +
r.status +
"). Check your account/access; no key is printed.",
);
const data = await r.json();
for (const model of data.models || []) {
if (
(model.supportedGenerationMethods || []).includes("generateContent") &&
typeof model.name === "string"
)
names.add(model.name.startsWith("models/") ? model.name.slice(7) : model.name);
}
token = data.nextPageToken || "";
pages++;
} while (token && pages < 10);
console.log(
[...names].sort().join("\n") ||
"No generateContent text-model candidates were returned for this account.",
);
if (token)
console.log(
"The listing stopped after ten pages; consult the current model documentation for the remainder.",
);
console.log(
"These are account-visible generation candidates, not a guarantee of free usage. Check modality, availability, pricing and quota before selecting one.",
);