/bolt.new-ai-integrations

Bolt.new AI and OpenAI GPT integration: Step-by-Step Guide 2025

Learn how to integrate Bolt.new AI with OpenAI GPT with our step-by-step guide. Discover tips, best practices, and expert insights to boost your AI projects.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to integrate Bolt.new AI with OpenAI GPT?

 

Setting Up Environment Variables for OpenAI API

 
  • Create an environment variable for your OpenAI API key. Since Bolt.new AI does not have a terminal, add the following code at the beginning of your main file (e.g., index.ts) to set up the variable. Replace YOUROPENAIAPI_KEY with your actual key:
  • 
    process.env.OPENAIAPIKEY = "YOUROPENAIAPI_KEY";
        
  • This makes the API key available throughout your project.

 

Creating the OpenAI GPT Integration Module

 
  • Create a new file called gpt.ts in your project. This file will contain the TypeScript function that calls the OpenAI GPT API.
  • Insert the following code into gpt.ts:
  • 
    const openaiApiKey = process.env.OPENAIAPIKEY;
    
    

    export async function getGPTResponse(prompt: string): Promise {
    const endpoint = "https://api.openai.com/v1/chat/completions";
    const headers = {
    "Content-Type": "application/json",
    "Authorization": Bearer ${openaiApiKey}
    };

    const body = {
    model: "gpt-3.5-turbo",
    messages: [{ role: "user", content: prompt }],
    temperature: 0.7
    };

    try {
    const response = await fetch(endpoint, {
    method: "POST",
    headers,
    body: JSON.stringify(body)
    });
    if (!response.ok) {
    throw new Error(Error ${response.status}: ${response.statusText});
    }
    const data = await response.json();
    return data.choices[0].message.content;
    } catch (error) {
    console.error("Error fetching GPT response:", error);
    return "";
    }
    }



  • Note: If your runtime does not support the native fetch API (Node.js versions lower than 18), insert the following line at the top of gpt.ts to import the fetch polyfill:

    import fetch from "node-fetch";

    Then, ensure that you add the dependency manually in your project’s dependency file if needed.

 

Integrating the GPT Module with Your Main Code

 
  • In your main file (e.g., index.ts), import the function from gpt.ts by adding the following line near the top:
  • 
    import { getGPTResponse } from "./gpt";
        
  • Create an endpoint or a function call where you want to use the GPT response. For example, if you have a button click or a form submission, add a function like below:
  • 
    async function handleUserPrompt(userPrompt: string) {
      const gptResponse = await getGPTResponse(userPrompt);
      console.log("GPT response:", gptResponse);
      // Further process the response or display it in your UI
    }
        
  • Call handleUserPrompt("Your prompt text here") from the appropriate part of your application to trigger the integration.

 

Deploying and Testing Your Integration on Bolt.new AI

 
  • Save all your changes in the Bolt.new AI editor.
  • No terminal commands are required since Bolt.new AI handles dependencies and deployment automatically.
  • Click on the Run button or deploy your project based on your workspace's instructions.
  • Trigger the handleUserPrompt function within your application to test if the GPT API responds correctly. Check the console log output for the response from GPT.

 

Additional Notes

 
  • Ensure that your OpenAI API key is kept secure and not exposed publicly.
  • If you need to add further dependencies (such as node-fetch for older Node versions), follow Bolt.new AI’s instructions for adding dependencies via code comments or project settings since the platform does not support a command-line interface.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022