# GPT-4 API: Hands-on guide

In this guide, I show how to start using GPT-4 API.

Table of Contents

*   [Get Access to GPT-4 API](https://harishgarg.com/#Get_Access_to_GPT-4_API)
*   [Call GPT-4 API from Python code](https://harishgarg.com/#Call_GPT-4_API_from_Python_code)

Get Access to GPT-4 API
-----------------------

First, you need to get access. Follow this [**guide to get access to GPT-4 API**](https://harishgarg.com/writing/how-to-get-access-to-gpt-4/).

Once, you have access, grab your [OpenAI API Key](https://platform.openai.com/account/api-keys).

Call GPT-4 API from Python code
-------------------------------

First, install openai python package using the command

pip install openai

Next, open a python file and type in the below code.

import openai

openai.api\_key = "your openai api key"

user\_prompt = "your prompt here"

openai.ChatCompletion.create(
   model="gpt-4",
   messages=\[
      {"role": "user", "content": user\_prompt}
   \]
)

print(response.choices\[0\]\["message"\]\["content"\])

Make sure to replace the below things in your code before proceeding further:

*   “your openai API key” with your openai API key
*   “your prompt here” with a prompt on what you want gpt-4 to do for you. Save the .py file

Next, run the python program from the command like this:

python your\_python\_file\_name.py

It will print out the response from the OpenAI GPT-4 API in your command line program.

You could also use the same code in a Google Colab or a Jupyter Notebook.

This was a very basic example of calling GPT-4 API from your python code. I will be posting more examples on advanced use cases. Check back again soon.
