Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ctrlaltdelme's avatar

Internal API Routes?

I'm using Vue + Inertia on a hobby project for teams at work and I'm trying to make a sort of link preview functionality and I was thinking of creating an internal API route that does the heavy lifting using a compostable. But, I'm not really sure an internal API route is the best way to do this or if there's another "better" way that sticks to best practices and convention.

If I do an internal API route, how would I define it? Do I need Sanctum? Is an internal API route even needed here? And I would like to try to be security-minded too

Any help is appreciated!

1 like
5 replies
martinbean's avatar

@ctrlaltdelme An internal API route to do what, exactly? It’s hard to say given we don’t know what you’re intending this API route to do.

1 like
ctrlaltdelme's avatar

@martinbean Ah sorry. To perform the request to get the OpenGraph details for the link preview (image, description, etc)

1 like
ctrlaltdelme's avatar

@martinbean when I tried implementing this yesterday, I made a post route to a api/link-preview route but kept getting csrf token errors. I think that's expected, but would I make a GET instead?

1 like
NekaDava's avatar

@ctrlaltdelme

Hi :) ,

For GET routes you don't need a csrf token, but for POST routes the csrf token is mandatory, you can load it through prop with php function {{ csrf_token() }} or add it entirely to the html within <head> tags in main app layout and take it from there <meta name="csrf-token" content="{{ csrf_token() }}"> . For small applications, especially for educational purposes, you won't need Sanctum. I hope this helps you :))

P.S. Using Inertia, it is possible to have a cleaner way to load the csrf token, but I have no experience and am not familiar with it.

1 like
ctrlaltdelme's avatar

@NekaDava Thanks for the reply! The Inertia docs actually warn against specifying a CSRF Token meta tag, so I don't know if that's the right solution. I think I have the right flow down but can't figure out how to implement it.

  1. View makes POST Request to my own application's route (can be /api or not, idk if it matters)
  2. Server then makes a GET request to the provided URL to get the metadata
  3. Server returns that to the Client to be displayed in a component and display the link preview

But my problem is whenever I implement it, even at a basic level to try to print the response from the website that is being requested via the GET, I get errors. Most of the time I was seeing a CSRF Token mismatch error, and I couldn't really figure out how to solve it from reviewing the Inertia docs.

Please or to participate in this conversation.