API Security Best Practices
API Security Best Practices
blog.bearer.sh/api-security-best-practices
31 March 2020
By nature, APIs are meant to be used. Even if all of your users are internal, security
problems can still arise. To help with this, we've assembled a list of best practices to
keep in mind when securing and locking-down an API or web service.
Use HTTPS
The web has moved past standard HTTP. With browser vendors flagging URLs that
don't use a secure layer, it's time to do the same for your API. HTTPS uses Transport
Layer Security (TLS) to encrypt traffic. This means communication between the client
and server is encrypted. For your API, this means the content sent from your API is
secured from third-parties, but more importantly it means that the access credentials
are secured.
Authenticate
Speaking of access credentials, the clearest way to avoid unexpected use of your API is
to ensure proper authentication. Authentication is how you allow or prevent access to
the API. Even publicly available APIs that are free to use should consider an
authentication strategy. This allows you to limit or remove users that abuse the API, and
also protect your users by giving them the ability to reset their credentials if needed.
You can learn more about types of API authentication in our article on the three most
common authentication methods.
1/4
Authorize
Authentication's sibling is authorization. Where authentication is concerned with who
the user of your API is, authorization focuses on what they have access to. For example,
free plan users may only be authorized to access a subset of your full API. When you
think about integrations like social login, the user authorizes your application to read
their profile data from the social platform.
Rate limit
When we think of security, we often think of inappropriate access. It can also be useful
to think of security as managing resources. Rate-limiting is a technique for throttling
the usage of an API. It can protect your resources financially, but also ensure that your
servers aren't overloaded by a flood of requests at one time. Many rate-limiting
approaches are time-based. They can be set for a billing period to handle overall usage,
as well as use a "burst" approach to limit large influxes of requests. If you've ever seen
the 429 HTTP status code, you've experienced rate-limiting.
2/4
Configure error messages
In addition to sanitizing data that goes into your API, you'll want to sanitize the
information that comes out of it. Error messages play a key role in helping users
understand that a problem occurred, but make sure not to leak any sensitive data.
Providing end-users with details about the structure of your internal code can open up
areas for attackers to focus on. Make sure to configure error messages to provide
enough information to help users debug and enough for them to report problems, but
not enough to expose the inner workings of your application or sensitive data.
3/4
In addition to these best practices, consider adopting recommendations from The Open
Web Application Security Project (OWASP). They offer platform-specific guides as well
as an upcoming API-specific guide, The API Security Top 10 . Beyond securing your API
on a code-level, you'll also want to ensure that your servers and infrastructure are
configured properly to avoid unauthorized access.
4/4