React 19, Using Assistant ID, Endpoint , CORS

Im able to connect to the regular GPT using the API from localhost, npm start. using React-19 and Expo. When I try to use the assistant ID I can’t figure out if the endpoint is incorrect or if there is a CORS issue with Assistants. I’m thinking if regular GPT API calls work that CORS shouldn’t be the issue. Here’s the code. Local host is http vs https, but again regular calls to API work. Tried CURL PHP and now NODE js. Funny thing is I was pulling from thread ID on the PHP attempt. These are two projects but the Node.JS. React-19 is the code here.

// src/ChatForm.js
import React, { useState } from ‘react’;
import axios from ‘axios’;

const ChatForm = () => {
const [inputText, setInputText] = useState(‘’);
const [response, setResponse] = useState(‘’);
const [loading, setLoading] = useState(false);

const handleSubmit = async (event) => {
event.preventDefault();
setLoading(true);
try {
const apiKey = ‘KEY-HERE’; // Replace with your actual OpenAI API key
const assistantId = ‘ID-here’; // Replace with your Assistant ID from the Playground
const result = await axios.post(
https://api.openai.com/v1/assistants/${assistantId}/completions,
{
messages: [
{ role: ‘user’, content: inputText },
],
max_tokens: 150,
},
{
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${apiKey},
},
}
);
setResponse(result.data.choices[0].message.content);
} catch (error) {
console.error(‘Error fetching the OpenAI API:’, error.response ? error.response.data : error.message);
setResponse(‘An error occurred. Please try again.’);
} finally {
setLoading(false);
}
};

return (



<input
type=“text”
value={inputText}
onChange={(e) => setInputText(e.target.value)}
placeholder=“Ask the assistant something…”
/>
Send

{loading &&
Loading…
}
{response &&
Response:

{response}

}

);
};

export default ChatForm;

Hi

CORS is a browser secuity thing. Calling stuff from an node server and calling things from your browser (react) are two different things.

If you want, you can look up how to disable cors on your browser, but it’s probably not a good idea to use that browser on the regular internet.

If you want to serve this app to other people, it’s a good idea to proxy your requests through a node server. never put your api key in any code that the browser sees.

1 Like

Appreciate you.

I’m just in development right now, not production. The CORS issue was a GPT suggestion, but I believe that if the regular connection was made from the same http local host it shouldn’t be an issue with an assistant. Let me know if you see or have a script I can study.
Using React or PHP I can connect to regular prompts. They both fail on assistants for me.

PHP fails on assistants? What error do you get? Can’t be cors…

Aah I think I see

All this is ChatGPT code.

This is how you actually post messages to an assistant thread: https://platform.openai.com/docs/api-reference/messages

Here’s a tutorial: https://platform.openai.com/docs/assistants/overview/agents

2 Likes