0%
Configuring the ChatGPT API

API Configuration

Setting up the ChatGPT API

10-15 min

Configuring the ChatGPT API

Getting an API key

To use the ChatGPT API, you first need to get an API key:

  1. Create an account on OpenAI
  2. Go to the "API Keys" section in your dashboard
  3. Generate a new API key
  4. Store this key somewhere safe (it will only be displayed once)

Basic configuration

Here is how to configure the ChatGPT API in your application:

// Configuration de base
const OPENAI_API_KEY = 'votre-clé-api';
const API_URL = 'https://api.openai.com/v1/chat/completions';

// Fonction pour appeler l'API
async function callChatGPT(prompt) {
  try {
    const response = await fetch(API_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${OPENAI_API_KEY}`
      },
      body: JSON.stringify({
        model: 'gpt-3.5-turbo',
        messages: [
          { role: 'user', content: prompt }
        ],
        temperature: 0.7,
        max_tokens: 1000
      })
    });

    const data = await response.json();
    return data.choices[0].message.content;
  } catch (error) {
    console.error('Erreur lors de l\'appel à l\'API:', error);
    throw error;
  }
}

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!