How to work with ChatGPT using the OpenAI API and Python

By Volodymyr Obrizan on April 28, 2025 · Read this post in other languages: Ukrainian Russian

Want to create your own intelligent assistant or integrate ChatGPT into your website or app? In this article, we will step-by-step explain how to work with the OpenAI API using Python. You will learn what ChatGPT and the OpenAI API are, how to register on the OpenAI platform, generate your first API key, make your first request via curl, and create your first small Python app to interact with ChatGPT. Everything is explained as clearly as possible and with plenty of screenshots.

What are ChatGPT and the OpenAI API?

OpenAI API is a programming interface for interacting with powerful language models created by OpenAI, such as ChatGPT, GPT-4, and others. The API allows you to programmatically send text requests and receive generated responses, enabling you to integrate AI capabilities into your own applications, websites, or automation processes.

ChatGPT is a model specially trained to conduct dialogues, answer questions, assist in writing texts, translate languages, generate creative ideas, and solve many other tasks. It is built on the GPT (Generative Pre-trained Transformer) architecture and optimized for interaction in a conversational format.

Using the OpenAI API, you can:

  • receive detailed responses to queries;
  • create personalized virtual assistants;
  • automate text processing;
  • develop intelligent chatbots for websites and services.

Communication with the API is done via HTTP requests, and responses are returned in JSON format. To get started, all you need is to register an account on OpenAI, obtain an API key, and have basic Python skills.

In the following sections, we will look at how to do this step by step.

Registering on the OpenAI Developer Platform

  1. Go to: https://platform.openai.com/
  2. Click Sign up.

    OpenAI Developer Platform homepage

  3. Choose a convenient registration option, for example, using your Google email.

    Create account at OpenAI

  4. Fill in your information and click Continue:

    • Full name — first and last name;
    • Birthday — date of birth. Note that in American format, the month is first, then the day: //.

    Tell us about you

  5. On the Welcome to OpenAI Platform page, fill in information about your organization and click Create organization:

    • Organization name — name of your organization. You can leave it as "Personal" (meaning you plan to use the platform for personal purposes);
    • What best describes you? — what best describes you? This question is about your technical skills. You can leave it as "Somewhat technical" (meaning you have some technical knowledge).

    Welcome to OpenAl Platform

  6. On the Invite your team page, click I'll invite my team later.

    invite your team

  7. On the Make your first API call page, fill out the form and click Generate API Key:

    • API key name — the name of the API key, can be "My Test Key". API keys belong to projects to help you manage usage limits, team access, and data security.
    • Project name — the name of the project where this API key will be created. You can leave it as "Default project".

    generate api key

Congratulations! 🚀 At this step, you have successfully registered on the OpenAI Developer Platform and obtained an API key. The API key is a secret string that starts with sk-proj-... and is about 160 characters long. It looks approximately like this:

sk-proj-ieqbTxAWEF0GDSgfv4K0m27o2AW34EF5LhNjvl_QH21Uz234123NCJT3BlbkFJXh4rnaoAPRAWEFAWEAaK8Xl9fHmSOmASEDFASDhBV9AUASDFyq6L_3sROehQhz4afQWAWEASDFt9

api key generated

⚠️ IMPORTANT!

  1. Copy the API key from this page because it will not be shown again (since it is secret). If you lose the key, you can create a new one.
  2. Store this API key in a secure place and do not share it with anyone, because having this API key allows performing actions with ChatGPT on your behalf and accessing all information about your project on the OpenAI Developer Platform.

More about safe handling of API keys: Best Practices for API Key Safety

After generating the API key, the Add some API credits page will open. Using ChatGPT via the API is paid. You can click I'll buy credits later or make a minimum deposit of $5 using a payment card.

add credits

Request to ChatGPT using curl

Now you can test programmatic integration with the OpenAI API using the terminal and the curl utility, or proceed to the next step and try it immediately with Python.

Let's ask ChatGPT to write a poem. Prompt: write a poem about artificial intelligence.

Open your terminal, copy the code below. Replace sk-proj-XXXXXXXXXXXXXXXX with your API key obtained in the previous step. This example uses the GPT-4o mini (gpt-4o-mini) model.

Run this command in the terminal.

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-proj-XXXXXXXXXXXXXXXX" \
  -d '{
    "model": "gpt-4o-mini",
    "store": true,
    "messages": [
      {"role": "user", "content": "write a poem about artificial intelligence"}
    ]
  }'

013-openai-curl-command-line.png

After a short delay (a few seconds) and if successful, a JSON-formatted response will appear in the terminal. The document is complex with many service keys, but the most important is "content" (in the red rectangle). This is the ChatGPT response.

013-openai-curl-answer.png

Request to ChatGPT using Python

But it is more interesting to work with ChatGPT using the Python programming language because you can implement many useful algorithms and applications based on the OpenAI API. Let's make the simplest Python app that works with ChatGPT using the OpenAI API. We will show this example using JetBrains PyCharm IDE, but you can use your favorite IDE.

  1. Install PyCharm from: https://www.jetbrains.com/pycharm/download/
  2. Launch PyCharm and click New Project to create a new project:

    pycharm create new project

  3. In the new project creation dialog, you can leave everything as is or specify the project location on your local disk (field Location).

  4. Click Create.

    new project

  5. Right-click on the folder with the project name and choose New > Python File from the menu:

    new python file

  6. Name the file, for example: main.py:

    main py

  7. Copy the example content into the new file. Be sure to replace sk-proj-XXXXXXXXXXXXX with your API key obtained in the previous step:

from openai import OpenAI

client = OpenAI(
    api_key="sk-proj-XXXXXXXXXXXXX"
)

response = client.responses.create(
    model="gpt-4o-mini",
    input="write a poem about artificial intelligence"
)

print(response.output_text)

paste contents

  1. Open the terminal window via the menu View > Tool Windows > Terminal:

    open terminal

  2. In the terminal window, type the command pip install openai and press Enter. This will install the OpenAI library in your project:

    pip install

  3. After a few seconds, the OpenAI library will be installed in your project:

    pip install result

  4. On the toolbar, click the Debug button (green bug):

    debug

  5. Wait a few seconds, and after successful execution, the response from ChatGPT (model gpt-4o-mini) will appear in the console:

    output

🚀 Congratulations! You have successfully made a programmatic request to ChatGPT using the OpenAI API and Python! 👏

Poem about Artificial Intelligence

Here is the full poem ChatGPT wrote in response to the prompt "write a poem about artificial intelligence":

In a world of bright thoughts and ideas,
Machines roam with fires in their hearts,
Artificial mind unfurls its sails,
In the boundless knowledge, new paths it charts.

It can create poems from scratch,
Reveal secrets, depths, and ways,
Mysteries of time, space, and eras,
It seeks meaning, answers through the days.

Technology tightly binds the world,
Humans and machines now walk side by side,
Will it become a friend, or brother at last,
Or a threat in the struggle as time will decide?

Dreams of the future already roam the air,
With experience and wisdom it teaches, leads,
But let us not forget what lives in the heart,
Immortal love and warm human seeds.

Artificial intelligence, blue-bright beacon,
Lead us to truth, opening the way,
But always remember — what’s in our hands,
Only we can decide what in our hearts will stay.

Write in the comments what poem ChatGPT wrote for you?

Now you know how to quickly register on the OpenAI Developer Platform, create an API key, test it via curl, and create a basic Python app to communicate with ChatGPT. This is the first step towards creating your own intelligent solutions, chatbots, automation systems, or creative projects using artificial intelligence. From here, everything depends only on your imagination and practice! 🚀

Telegram
Viber
LinkedIn
WhatsApp

Comments

Sign in to leave a comment.

← Back to blog