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

msaied's avatar
Level 10

How to Secure an API?

Hello, everyone ♥ I build a web application with API with two types ( public & private) the private one is secured by Authintcation ( sanctum) my question is how to secure public one to be sure no one else can use it except the application

0 likes
8 replies
msaied's avatar
Level 10

@bugsysha Do you mean sanctum? if yes! how-to, because this API is used by phone app so how to auth phone app?

I think about a token to pass in a header from the app so I can validate this request, but is it the best practices

bugsysha's avatar

@msaied that can be sniffed. The best way is to have an account for each of the users using your public API.

msaied's avatar
Level 10

@bugsysha Reson of my question is I made an e-commerce website so I don't want anyone has an end-point to get JSON data as example for products i need to secure every thing and save server resources as well

martinbean's avatar

Reson of my question is I made an e-commerce website so I don't want anyone has an end-point to get JSON data as example for products i need to secure every thing and save server resources as well

@msaied Well, guess what? You have to use authentication.

You can’t have an API publicly accessible on the Internet that magically blocks access to every one but a particular mobile app. The mobile app needs to use some sort of authentication mechanism to identify itself so the API can go, “Oh, you’re the mobile app? Cool, I’ll let you access endpoints.”

If you don’t have authentication, then any one can just watch traffic between your server and the mobile app, get the API endpoint URLs, and start making requests themselves.

msaied's avatar
Level 10

@martinbean Thanks for your reply, I already use authentication for users, But my exact question is " how to secure connection between the mobile app and my website "

martinbean's avatar

I already use authentication for users, But my exact question is " how to secure connection between the mobile app and my website "

@msaied With. Authentication.

You can authenticate apps as well as users. So you don’t to create some form of authentication mechanism between your mobile app and your API.

1 like

Please or to participate in this conversation.