© 2026 Hedgehog Software, LLC
Twitter
GitHub
Discord
System
Light
Dark
More
Communities
Docs
About
Terms
Privacy
Search
Star
Feedback
Setup for Free
Failed to load resource: the server responded with a status of 500 () - Cloudflare Developers
CD
Cloudflare Developers
•
17mo ago
Noam
Failed to load resource: the server responded with a status of 500 ()
I am using Atsro trying to make an endpoint that accepts a message that returns a response
. Here is the code
:
import type
{ APIContext
} from
'astro
'
;
import OpenAI from
'openai
'
;
const openai
= new OpenAI
(
{
apiKey
: import
.meta
.env
.OPENAI
_API
_KEY
,
}
)
;
const ASSISTANT
_ID
= import
.meta
.env
.ASSISTANT
_ID
;
async function createAndRunThread
(message
: string
)
{
const thread
= await openai
.beta
.threads
.create
(
{
messages
:
[
{ role
:
'user
'
, content
: message
}
]
,
}
)
;
const run
= await openai
.beta
.threads
.runs
.createAndPoll
(thread
.id
,
{
assistant
_id
: ASSISTANT
_ID
,
}
)
;
if
(run
.status
=
=
=
'completed
'
)
{
const messages
= await openai
.beta
.threads
.messages
.list
(thread
.id
)
;
const assistantMessage
= messages
.getPaginatedItems
(
)
.find
(msg
=
> msg
.role
=
=
=
'assistant
'
)
;
/
/ Extract text content and clean up citations
let content
= assistantMessage
?
.content
[0
]
?
.type
=
=
=
'text
'
? assistantMessage
.content
[0
]
.text
.value
:
'
'
;
/
/ Remove citations using regex
content
= content
.replace
(
/
【
[
^
】
]
*
】
/g
,
'
'
)
;
return
{
content
}
;
}
throw new Error
(
Run failed with status: ${run.status}
Run failed with status: ${run.status}
)
;
}
export async function GET
(
{ url
}
: APIContext
)
{
try
{
const message
= url
.searchParams
.get
(
'message
'
)
;
if
(
!message
)
{
throw new Error
(
'Message is required
'
)
;
}
const thread
= await createAndRunThread
(message
)
;
return new Response
(JSON
.stringify
(thread
)
,
{
status
: 200
,
headers
:
{
'Content
-Type
'
:
'application
/json
'
}
,
}
)
;
} catch
(error
: unknown
)
{
const errorMessage
= error instanceof Error
? error
.message
:
'An unknown error occurred
'
;
return new Response
(JSON
.stringify
(
{ error
: errorMessage
}
)
,
{
status
: 500
,
headers
:
{
'Content
-Type
'
:
'application
/json
'
}
,
}
)
;
}
}
Has anyone solve this issue
? Previous comments haven
't provided solutions that worked
.
Cloudflare Developers
Join
Welcome to the official Cloudflare Developers server. Here you can ask for help and stay updated with the latest news
86,942
Members
View on Discord
Resources
ModelContextProtocol
ModelContextProtocol
MCP Server
Similar Threads
Was this page helpful?
Yes
No
Recent Announcements
Similar Threads
SvelteKit - Failed to load resource: the server responded with a status of 500 ()
CD
Cloudflare Developers / pages-help
3y ago
Deployment logs to understand `Status: Failed`?
CD
Cloudflare Developers / pages-help
3y ago
Deployment status "Failed" with no visible errors
CD
Cloudflare Developers / pages-help
2y ago
Getting a 500 internal server error
CD
Cloudflare Developers / pages-help
2y ago