Dino Geek, try to help you

How to connect to ChatGPT API with Node.js?


In order to connect to the ChatGPT API with Node.js, you can use the “axios” or “request” libraries for generating HTTP requests. Here is an example where axios is used:

1. You’d first need to install axios, which can be done using npm (Node Package Manager):

```
npm install axios
```

1. Afterwards, you can use the following code for a POST request to the OpenAI Chat API:

```
const axios = require(‘axios’);

const url = “https://api.openai.com/v1/engines/davinci-codex/completions”;

const generateText = async () => { const res = await axios.post(url, { “model”: “text-davinci-002”, “messages”: [ {“role”: “system”, “content”: “You are a helpful assistant.”}, {“role”: “user”, “content”: “Who won the world series in 2020?”} ] }, { headers: { ‘Authorization’: ‘Bearer YOUR_OPEN_AI_KEY’, ‘Content-Type’: ‘application/json‘ }} ); return res.data;
};

generateText().then(data => console.log(data)).catch(err => console.error(err));
```

1. In the above code, replace ‘YOUR_OPEN_AI\_KEY’ with your actual OpenAI key.

The above code will return a JSON object from the OpenAI ChatGPT API.

Note, you will need to use your OpenAI key, and remember to keep it secure. Never share it in public and don’t store it in your scripts or repositories. Instead, use environments variables or a secure method of storing configuration keys.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use