# ChatGPT integration

The [code editor](/en/documentation/platform/functions/code-editor/) of a function calls the [ChatGPT](https://openai.com/blog/chatgpt) APIs. The integration explains, generates, and refactors JavaScript inside the editor, so you do not open a separate tab. It runs on the credentials of your own OpenAI account, which you declare in the source code of the function.

---

## Credentials

The ChatGPT integration authenticates with an OpenAI API key and an OpenAI organization ID. Generate both on the OpenAI platform:

- [OpenAI API keys](https://platform.openai.com/account/api-keys)
- [OpenAI organization settings](https://platform.openai.com/account/org-settings)

Declare the two values as a comment in the source code of the function:

```javascript
/*
ChatGPTKey=[TOKEN VALUE]
ChatGPTOrg=<your-organization-id>
*/
```

---

## Code explanation

Code explanation returns a description of the code in the editor. Use it when you debug a function, when you review one before a change, or when you read code you did not write.

---

## Code generation

Code generation turns a comment into JavaScript. The **ChatGPT: Generate** command, in the context menu of the editor, reads the selected comment as the prompt and returns the generated code.

This prompt asks for an HTML table with three columns:

```javascript
// generate an html table with name, phone number and address
```

The integration returns the markup:

```html
<body>
  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Phone Number</th>
        <th>Address</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John Smith</td>
        <td>(123) 456-7890</td>
        <td>123 Main St, Anytown, USA</td>
      </tr>
      <tr>
        <td>Jane Doe</td>
        <td>(987) 654-3210</td>
        <td>456 Oak St, Anytown, USA</td>
      </tr>
      <tr>
        <td>Bob Johnson</td>
        <td>(555) 555-1212</td>
        <td>789 Maple St, Anytown, USA</td>
      </tr>
    </tbody>
  </table>
</body>
```

---

## Code refactor

Code refactor rewrites a block of JavaScript. The **ChatGPT: Refactor** command, in the context menu of the editor, refactors the code you select.

> **Tip**
>
> Test the code the integration returns before the function serves traffic. [Preview deployment](/en/documentation/platform/functions/preview-deployment/) returns the response of the generated or rewritten code without a deployment.

---

## Debugging

The integration also covers debugging. For the symptoms of a function that fails at run time, and the remedy for each, refer to [Troubleshooting](/en/documentation/platform/functions/troubleshooting/).

---

## Related resources

- [Code editor](/en/documentation/platform/functions/code-editor.md): The editor the integration runs in, and the rest of its features.
- [Preview deployment](/en/documentation/platform/functions/preview-deployment.md): Read the response of the generated code before it serves traffic.
- [Build an API function with ChatGPT](/en/documentation/guides/application-development/functions-and-runtime/api-builder.md): A worked example that generates an object and returns it as JSON.
- [How it works](/en/documentation/platform/functions/how-it-works.md): What happens to the code once a rule invokes the function.
- [Troubleshooting](/en/documentation/platform/functions/troubleshooting.md): Find the cause when a function fails or returns nothing.
- [Agent setup](/en/documentation/agent-setup.md): Use your own agent or editor against the Azion documentation instead.
