Integrating ChatGPT with OpenAI APIs: A Developer’s Guide

4 minute read

As the famous saying goes, “There’s more than one way to program a cat!” While I may be slightly misquoting, the point remains: there are many programming languages and methods to integrate ChatGPT with OpenAI APIs. This blog post will focus on integration for developers, using Ruby as an example. If you’re new to ChatGPT or need more context, we recommend checking out our previous blog posts in the series.

Please note that this post is on the technical side, and I’ll be providing an example in Ruby, a language I’m familiar with. If you’re comfortable with that, let’s jump right in and explore how to supercharge your applications with ChatGPT!

Important note: Some users might encounter an error message while making API calls, stating, “You exceeded your current quota, please check your plan and billing details.” If you face this issue, ensure you are within the usage limits of your OpenAI API subscription plan, and verify your billing information.

The OpenAI API utilizes a variety of models that offer distinct features and pricing options. Additionally, it allows for minor adjustments to our initial base models in order to cater to your unique requirements. For more information on the OpenAI API, please visit our pricing page.

Getting Started with OpenAI APIs

Before diving into the code, make sure you have access to the OpenAI API documentation and your API key. Once you have your API key, you’re ready to start making API requests and harness the power of ChatGPT.

Building a ChatGPT Integration with Ruby

To create a ChatGPT integration using Ruby, you’ll need to send API requests, handle API responses including the nil response case, and implement error handling.

Constructing And Sending API Requests

To interact with ChatGPT, you’ll need to make POST requests to the /v1/engines/gpt-3.5-turbo/completions endpoint. Here’s an example of a Ruby API request using the net/http library:

require 'net/http'
require 'uri'
require 'json'

# Set your API key
API_KEY = 'insert-api-key-here'
# Set the API URL
API_URL = ''

# Set up the request headers
headers = {
  'Authorization' => "Bearer #{API_KEY}",
  'Content-Type' => 'application/json'

# Initialize the HTTP object with the URI and enable SSL
uri = URI.parse(API_URL)
http =, uri.port)
http.use_ssl = true

# Set up the request data
data = {
  prompt: "What are the benefits of meditation?",
  max_tokens: 50,
  n: 1,
  stop: nil,
  temperature: 0.8

# Create a POST request with the URI, headers, and JSON data
request =, headers)
request.body = data.to_json

# Send the request
response = http.request(request)

Handling API Responses and Sample Response

When you send an API request, you’ll receive a JSON response containing either generated text or an error message. Here’s a sample success response:

# Sample success response
  "usage"=>{"prompt_tokens"=>6, "completion_tokens"=>50, "total_tokens"=>56},
       " Meditation has numerous benefits, including reduced stress, improved mental clarity, increased self-awareness, and enhanced emotional well-being. It can also help improve sleep quality, boost the immune system, and foster a greater sense of inner peace and relaxation.",

To extract the generated text from the response, parse the JSON and access the text key in the choices array, like this:

# Parse the JSON response
response_json = JSON.parse(response.body)

# Handle the response: check for success, print generated text or error message
generated_text = response_json['choices'][0]['text']
puts "Generated text: #{generated_text}"

Error Handling and nil Response Case

It’s essential to handle errors gracefully and account for the possibility of a nil response. In the example, we first check if the response object is not nil. If it is, we print an error message. If the response is not nil, we parse the JSON and check the HTTP status code. If the status code is 200, we extract the generated text. If the status code is not 200, we print the error message from the response.

# Check if the response is not nil
if response
  response_json = JSON.parse(response.body)

  # Handle the response: check for success, print generated text or error message
  if response.code == '200'
    generated_text = response_json['choices'][0]['text']
    puts "Generated text: #{generated_text}"
    puts "Error: #{response_json['error']['message']}"
  puts "Error: No response received."

Example Integrations

You can integrate ChatGPT into various platforms, tools, and frameworks using different programming languages, like Ruby in our example:

  • Web applications: Use ChatGPT to generate personalized content, answer user queries, or offer smart suggestions in your Ruby on Rails or Sinatra web application, or adapt the concept to other web development frameworks.
  • Messaging platforms: Integrate ChatGPT into messaging platforms like Slack, Telegram, or Discord to create chatbots that provide useful information or support, leveraging language-specific libraries or SDKs.
  • Content management systems: Enhance content creation and editing workflows in platforms like Jekyll or other language-based CMS by integrating ChatGPT as a content generation or editing assistant.


By integrating ChatGPT with OpenAI APIs, you can create smarter, more engaging, and efficient applications. This guide has given you the foundation for sending API requests and handling responses using Ruby as an example. With your newfound knowledge, you can explore various integration possibilities and expand the horizons of your projects, regardless of your preferred programming language. Happy coding!