A Node.js application demonstrating how to use OpenAI's Conversations API (/v1/conversations) to create and manage conversations.
- Create conversations with initial messages
- Add messages to conversations
- Update conversation metadata (e.g., title)
- Delete conversations
- Retrieve conversation history
- List conversation items and fetch specific items
- Node.js (v18 or higher) - Required for native
fetchAPI support - npm or yarn
- OpenAI API key (Get one here)
- Clone or navigate to this repository
- Install dependencies:
npm install- Create a
.envfile in the root directory:
cp .env.example .env- Add your OpenAI API key to the
.envfile:
OPENAI_API_KEY=sk-your-api-key-here
Backend (CLI example):
npm startRun the API server (for the React app):
npm run serverRun the React client (in another terminal):
npm run clientOr run both together (single package.json):
npm run dev:allThe application demonstrates a complete conversation flow:
- Creates a Conversation: Creates a new conversation with optional initial messages and title
- Adds Messages: Sends user messages to the conversation
- Retrieves Conversation: Gets the full conversation details
- Lists Conversations: Shows all available conversations
- Retrieves Messages: Gets the conversation history
The application includes the following functions:
createConversation(messages, title)- Creates a new conversationgetConversation(conversationId)- Gets a specific conversationupdateConversation(conversationId, updates)- Updates a conversationdeleteConversation(conversationId)- Deletes a conversationaddMessageToConversation(conversationId, role, content)- Adds a message to a conversationgetConversationMessages(conversationId)- Gets all messages from a conversationlistConversationItems(conversationId, options)- Lists items within a conversationgetConversationItem(conversationId, itemId)- Retrieves a specific item from a conversation
This application uses the OpenAI Conversations API endpoint:
- Base URL:
https://api.openai.com/v1 - Conversations Endpoint:
POST /conversations,GET /conversations, etc.
The React app calls the local Express server routes:
POST /api/conversationsPOST /api/conversations/:id/messagesGET /api/conversations/:id/items
You can modify the exampleConversation() function in index.js to:
- Change the system message and instructions
- Modify the user message
- Add more messages to the conversation
- Update conversation metadata
MIT