Skip to main content

OpenAI Integration

Add powerful AI capabilities to your app using OpenAI’s GPT models. Create chatbots, generate content, analyze text, and more.

What You Can Build

  • AI Chat — Conversational interfaces
  • Content Generation — Write text, summarize, translate
  • Q&A Bots — Answer questions about your content
  • Creative Tools — Story writing, brainstorming
  • Analysis — Sentiment analysis, categorization

Prerequisites

  • OpenAI account with API access
  • API key from OpenAI dashboard
  • Credit balance (new accounts get $5 free)

Getting Your API Key

1

Create OpenAI account

Go to platform.openai.com and sign up
2

Navigate to API keys

Click your profile → View API Keys
3

Create new key

Click “Create new secret key”
4

Copy and save

Copy the key immediately — it won’t be shown again!
Keep your API key secret! Anyone with your key can make API calls charged to your account.

Setting Up in Nativeline

Tell Nativeline about your API:
Or configure via settings if available.

Building an AI Chat

Basic Chat Interface

Connecting to OpenAI


OpenAI Models

Choosing a Model


API Parameters

Temperature

Controls randomness (0 = focused, 1 = creative):

Max Tokens

Limits response length:

System Prompt

Sets the AI’s behavior:

Example Use Cases

Chatbot

Content Generator

Summarizer

Writing Assistant


Handling Conversations

Maintaining Context

Include conversation history for context:

Trimming History

For long conversations, trim old messages:

Error Handling

Common Errors

Handling in App


Cost Management

Monitoring Usage

Check usage at platform.openai.com/usage

Reducing Costs

  • Use gpt-4o-mini instead of gpt-4o
  • Set max tokens limit
  • Cache common responses
  • Trim conversation history

Setting Limits

In OpenAI dashboard:
  1. Settings → Billing → Usage limits
  2. Set monthly budget
  3. Get notified before exceeding

Best Practices

Clear system prompts improve results:
  • Define the AI’s role
  • Specify tone and style
  • Set boundaries
  • Include examples
Implement exponential backoff:
  • Wait 1 second, retry
  • If fails, wait 2 seconds
  • Then 4, 8, etc.
API calls take time. Show:
  • Loading indicator
  • “Thinking…” message
  • Disable send button
Check user input before sending:
  • Not empty
  • Within length limits
  • No obvious abuse

Advanced: Streaming

For longer responses, stream tokens as they arrive:
This provides better UX for longer generations.

DALL-E (Image Generation)

OpenAI also offers image generation:
DALL-E has separate pricing. Check OpenAI pricing page for current rates.

Next Steps

Stripe Payments

Monetize your AI features

Other APIs

Generic API integration guide