codewithbeast
codewithbeast
  • Home
  • Blog
  • Categories
  • Contact Us
Subscribe
codewithbeast
codewithbeast
  • Home
    • Home 1
    • Home 2
    • Home 3
    • Home 4
  • Blog
    • Blog 01
    • Blog 02
    • Blog 03
  • Single Posts
    • Standard Format
    • Split Format
    • Overlay Format
    • Sidebar Left
    • Sidebar Right
    • Single Post
  • Pages
    • Author
    • Search Result
    • Contact Us
    • Social Media
    • 404
  • Account
    • Blog 01
    • Blog 02
    • Details Left
    • Details Right
    • Single Blog
  • Contact Us
Get A Quote

Get in touch

codewithbeast
contact@codewithbeast.com
codewithbeast
123 Innovation Drive,
Tech City, ST 12345, USA
codewithbeast
123-456-7890

Our Social Network

HomeBlogArtificial Intelligence

Build an AI-Powered App with the OpenAI API: Step-by-Step Tutorial

Mohammed Aman
Mohammed Aman
date 29 June 2025
time 14 min read

Build an AI-Powered App with the OpenAI API: Step-by-Step Tutorial

The OpenAI API gives you programmatic access to GPT-4o and other models. In this tutorial, you will build a streaming AI chatbot with Next.js from scratch — covering API setup, streaming responses, and deployment.

Build an AI-Powered App with the OpenAI API: Step-by-Step Tutorial

Getting Started with the OpenAI API

The OpenAI API provides programmatic access to GPT-4o, GPT-4o mini, DALL-E 3, Whisper (speech-to-text), and TTS (text-to-speech). You pay per token — roughly $0.15 per million input tokens for GPT-4o mini and $5 per million for GPT-4o. For most projects, GPT-4o mini provides 90 percent of the capability at 3 percent of the cost.

Create an account at platform.openai.com, navigate to API keys, and generate a new secret key. Store it as OPENAI_API_KEY in your environment variables — never hardcode it in your source code or commit it to Git. Install the SDK with npm install openai for JavaScript or pip install openai for Python.

Understanding the Chat Completions API

The core API call takes an array of messages, each with a role (system, user, or assistant) and content. The system message sets the AI's behavior and persona. User messages are what the human sends. Assistant messages are previous AI responses, which you include to maintain conversation history.

The response includes the generated message content, token usage (input and output counts for billing calculation), finish reason (stop means it completed normally, length means it hit the token limit), and the model used. Always log token usage in development to estimate production costs before launch.

Building a Streaming Chatbot with Next.js

Streaming shows responses word by word as they are generated rather than waiting for the full response. This dramatically improves perceived performance — users see the AI thinking rather than staring at a spinner. In Next.js, create an API route that calls the OpenAI API with stream: true and pipes the response directly to the client.

The frontend connects to this API route using the Vercel AI SDK (npm install ai), which handles streaming, parsing, and state management with React hooks. The useChat hook provides messages, input, handleInputChange, and handleSubmit — everything you need for a complete chatbot UI in about 20 lines of component code.

Adding Function Calling and Tool Use

Function calling lets the AI call your application's functions — fetching weather data, searching a database, looking up a user's account. Define functions with their name, description, and parameter schema. When the AI decides to call a function, it returns a structured object instead of plain text. Your code executes the function and returns the result, which the AI uses to formulate its final response.

This pattern is how AI agents work — the AI reasons about what information it needs, calls tools to get it, synthesizes the results, and responds. Tool calling transforms a simple chatbot into a capable AI assistant that can take real actions in your application. Add moderation using OpenAI's free moderation endpoint to flag harmful content before it reaches your application.

  • Tags:
  • OpenAI API
  • ChatGPT
  • Next.js
  • AI Development
  • Tutorial
  • Share:
Leave a Reply

Share your thoughts about this article.

Search

Mohammed Aman

Mohammed Aman

Tech blogger covering AI, coding, and the future of software. Founder of CodeWithBeast.

Categories

  • Artificial Intelligence
  • Machine Learning
  • Programming
  • Vibe Coding
  • Computer Science
  • Web Development
  • DevOps & Cloud
  • Cybersecurity
  • Open Source
  • Coding
  • Business & Tech
  • Tech

Recent Posts

Claude AI vs ChatGPT vs Gemini: Which AI Assistant Wins in 2025?
date 10 July 2025
Claude AI vs ChatGPT vs Gemini: Which AI Assistant Wins...
What is Vibe Coding? The AI Revolution Turning Everyone into a Developer
date 9 July 2025
What is Vibe Coding? The AI Revolution Turning Everyone...
Top 10 Programming Languages to Learn in 2025 (Ranked by Demand)
date 8 July 2025
Top 10 Programming Languages to Learn in 2025 (Ranked b...
Machine Learning for Beginners: Your Complete 2025 Guide
date 7 July 2025
Machine Learning for Beginners: Your Complete 2025 Guid...

More Articles

Claude AI vs ChatGPT vs Gemini: Which AI Assistant Wins in 2025?
Artificial Intelligencetime 8 min read

Claude AI vs ChatGPT vs Gemini: Which AI Assistant Wins in 2025?

The AI assistant landscape is more competitive than ever. Claude, ChatGPT, and Gemini each excel in ...

Prompt Engineering: 20 Advanced Techniques to 10x Your AI Productivity
Artificial Intelligencetime 11 min read

Prompt Engineering: 20 Advanced Techniques to 10x Your AI Productivity

The difference between a mediocre and exceptional AI output is usually the prompt. These 20 proven t...

Never Miss a Tech Article

Join thousands of developers getting the latest AI, coding, and tech tutorials delivered to their inbox every week.

CodeWithBeast
codewithbeast

CodeWithBeast is your hub for AI, coding, and the latest in tech. Empowering developers, creators, and learners worldwide.

Explore Categories

  • Artificial Intelligence
  • Machine Learning
  • Web Development
  • DevOps & Cloud
  • Cybersecurity
  • Open Source

Quick Links

  • Home
  • Blog
  • Categories
  • Contact Us
  • Privacy Policy
  • Terms of Service

Contact Us

codewithbeast
support@codewithbeast.com
codewithbeast
Street 3, Jamali Hills, Tolichowki
Hyderabad, Telangana 500019
codewithbeast
+91 9618477436

© 2026 CodeWithBeast. All Rights Reserved.

Privacy PolicyTerms & Conditions