API Security Best Practices: Authentication, Authorisation, Rate Limiting and More
How to secure REST and GraphQL APIs used by mobile apps and websites: tokens and keys, object-level authorisation, input validation, rate limits, CORS, webhooks and logging — with simple examples.

Table of contents
Your API is your app’s front door
Mobile apps and modern websites talk to the backend through APIs, and attackers can call those APIs directly — skipping your app’s screens and checks entirely. Anything the app hides, the API must still protect.

The biggest API risks
- Broken object-level authorisation: changing /orders/1001 to /orders/1002 shows someone else’s order.
- Broken authentication: weak tokens, tokens that never expire or missing checks on some endpoints.
- Excessive data exposure: returning whole database records and relying on the app to hide fields.
- No rate limiting: attackers brute-force logins, scrape data or run up SMS and AI costs.
- Mass assignment: users set fields they should not, such as role or price, by adding them to the request.
Best practices, step by step

- 1
Authenticate every request: use short-lived access tokens and refresh tokens, or secure session cookies for web apps.
- 2
Authorise every object: check ownership or role on the server for each record requested or changed.
- 3
Return only needed fields: use response schemas or DTOs instead of raw database rows.
- 4
Validate input with a schema: reject unknown fields and wrong types (for example with Zod or Joi).
- 5
Rate-limit and throttle: per user, per IP and per endpoint, with stricter limits on login, OTP and search.
- 6
Configure CORS narrowly: allow only your own domains for browser access.
- 7
Verify webhooks: check signatures from payment gateways and other providers before trusting the payload.
app.get("/api/orders/:id", requireAuth, async (req, res) => {
const order = await db.order.findUnique({ where: { id: req.params.id } })
if (!order || order.userId !== req.user.id) return res.status(404).end() // not 403: do not reveal it exists
res.json({ id: order.id, status: order.status, total: order.total }) // only needed fields
})Keys, secrets and mobile apps
“Assume every API endpoint will be called by someone who is not using your app.”
- No secrets in the app: anything inside a mobile app or JavaScript bundle can be extracted.
- Proxy third-party APIs: call payment, SMS and AI providers from your server, not from the client.
- Rotate keys: and revoke immediately if a leak is suspected.
- Log and monitor: track 401/403 spikes, unusual volumes and access to sensitive endpoints.
Topics
Umer Hasan
Co-Founder & CTO at Zapprex
Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.
Keep reading
All articlesOTP Login Done Right: Security, Rate Limits, Autofill and SMS vs WhatsApp OTP
Build OTP login that is secure, cheap and pleasant: how OTPs should be generated and stored, rate limiting, SMS pumping fraud, autofill on Android, iOS and the web, and when to use WhatsApp or voice OTP.
Web Application Security Checklist: 25 Must-Do Steps Based on the OWASP Top 10
A practical, plain-English security checklist for websites and web apps: access control, injection, authentication, secrets, dependencies, headers, logging and backups — mapped to the OWASP Top 10.
India’s DPDP Act Explained for App and Website Owners: What You Need to Do
A simple guide to India’s Digital Personal Data Protection Act and Rules: consent and notices, purpose limits, user rights, children’s data, breach reporting, retention and the product changes to plan.
Questions readers ask
JWTs can be secure when signed with strong keys, kept short-lived, validated properly on every request and paired with a revocation strategy such as refresh tokens.
Rate limiting restricts how many requests a user or IP can make in a time window, protecting against brute force, scraping and cost-based abuse.
Only public, restricted keys. Secret keys for payments, SMS or AI providers must stay on your server.
Have a project in mind?
Let's build something that moves your business forward.
Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.
- Founder reply within 1 business day
- NDA on request · 100% code ownership
- Talk directly to the engineers
