T
TanStack4mo ago
adverse-sapphire

Get API URL(link) from server function & API route

Hi team thanks for the awesome stack. I wonder if there is a typesafe way to get the URL of an api route, e.g. https://mydomain.com/api/thatCallback, from within a server function or API route? If not, just getting a relative path is also good. This will be useful for both API route and server function. Cheers.
1 Reply
adverse-sapphire
adverse-sapphireOP4mo ago
Problem solved. For your reference:
import { APIRoute as ThatRoute } from '~/routes/api/that'

const path = ThatRoute.path
import { APIRoute as ThatRoute } from '~/routes/api/that'

const path = ThatRoute.path
Also, i might have found a bug that if I try the same for non-api routes, I always get Route.to as undefined from Server function. There is way to walk around for me at the moment with redirect({to:}) from '@tanstack/react-router'

Did you find this page helpful?