Search code examples
openai-api

OpenAI API: How do I get a list of all available OpenAI models?


Can anyone find the cURL command to get a list of all available OpenAI models? I've been looking for like 10 minutes and can't find it.


EDIT: I got the answer, and I see the problem. It is one of those docs where everything is on one page.


Solution

  • Python

    from openai import OpenAI
    import os
    client = OpenAI(
        api_.key = os.getenv('OPENAI_API_KEY')
    )
    models = client.models.list()
    for model in models:
        print(model.id)
    

    Node.js

    const OpenAI = require("openai");
    
    const client = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY,
    });
    
    async function main() {
      const list = await client.models.list();
    
      for await (const model of list) {
        console.log(model);
      }
    }
    
    main();
    

    cURL

    curl https://api.openai.com/v1/models \
      -H "Authorization: Bearer $OPENAI_API_KEY"
    

    See the official OpenAI documentation.