r/SvelteKit • • Oct 21 '25
How to access server data on initialization of server
I am writing code that uses axios to make an http call initiated from server init hook. I use getRequestEvent in the interceptor to figure out the current request on the server. But I get:
Error: Can only read the current request event inside functions invoked during
handle, such as serverloadfunctions, actions, endpoints, and other server hooks.
I thought init is a server hook, why cannot I not access server request info?
Code snippets:
hooks.server.ts
export const init: ServerInit = async () => {
await ConfigService.LoadConfig() // long story short, calls an exios http client to make a call
}The axios interceptor:
httpClient.interceptors.request.use(
function (config) {
if (!browser) {
const s = getRequestEvent();
// error
}
return config;
},
// ...
);How do I access the server information if not through getRequetEvent?
2
Upvotes
2
u/DanielBurdock Oct 21 '25
I could be completely wrong here as I can't remember what gave me the same problem, but getting rid of async for the function that was giving me the same error made it work.
https://svelte.dev/docs/kit/$app-server#getRequestEvent
In environments without AsyncLocalStorage, this must be called synchronously (i.e. not after an await).
1
u/Nervous-Blacksmith-3 Oct 21 '25
One question, why are you using Axios? SvelteKit has native Fetch, for the project, normally using it reduces many errors in the logic of how the requests themselves work.
Hey, sometimes it's interesting to ask the AI to get some idea quick
GrokResponse:
Why getRequestEvent Fails in init