Skip to content

Commit cf114db

Browse files
add contents for REST API Headers, Security and Versioning
1 parent 83bf51f commit cf114db

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/rest-api/headers.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,20 @@ sidebar_label: Headers
55
---
66

77
#### The following convention should be followed for REST API Headers
8+
9+
* Headers names should be **noun** and should be **Captialised-Case** separated by **(-)**. e.g **Token-Key**, **Account-ID**, **Tenant-ID** etc
10+
11+
* For cases like Acronym, use acronyms itself. eg. *PID-value*, *PIN*
12+
13+
* Headers can be used for meta information that the API carries for e.g
14+
* Authentication
15+
* Authorization
16+
* Versioning the API
17+
* Content-Type
18+
* Caching etc
19+
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank">More Examples</a>
20+
21+
* Avoid using headers for business logic
22+
23+
* Use headers for parameters that should not appear in the URL
24+
* for e.g https://api.application.com/users/{id}/fetch?apiKey=abcd123456789 //**BAD Practice**

docs/rest-api/security.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,23 @@ title: Security Method
44
sidebar_label: Security
55
---
66

7-
#### Security
7+
## Best Practices to Secure REST APIs
8+
9+
### Always Use HTTPS
10+
11+
* By always using SSL, the authentication credentials can be simplified to a randomly generated access token that is delivered in the username field of HTTP Basic Auth. It’s relatively simple to use, and you get a lot of security features for free.
12+
13+
* If you use HTTP 2, to improve performance – you can even send multiple requests over a single connection, that way you avoid the complete TCP and SSL handshake overhead on later requests.
14+
15+
### Never expose information on URLs
16+
17+
* Usernames, passwords, session tokens, and API keys should not appear in the URL, as this can be captured in web server logs, which makes them easily exploitable. e.g
18+
https://api.app.com/users/{id}/fetch?apiKey=abcd123456789 //Bad practice
19+
20+
### Conside using token based authentication like OAUTH2
21+
22+
### Consider Adding Timestamp in Request
23+
24+
### Log each request and response data
25+
26+
### Validate Incoming request

docs/rest-api/versioning.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,32 @@ title: Versioning
44
sidebar_label: Versioning
55
---
66

7-
#### The following convention should be followed for Versioning
7+
## Four common REST API versioning strategies
8+
9+
### Versioning through URI Path (*recommended*)
10+
11+
> <http://api.app.com/v1/products>
12+
>
13+
> One way to version a REST API is to include the version number in the URI path.This solution often uses URI routing to point to a specific version of the API. Because cache keys (in this situation URIs) are changed by version, clients can easily cache resources. When a new version of the REST API is released, it is perceived as a new entry in the cache.
14+
15+
### Versioning through custom header
16+
17+
> curl -H API-Version: 1.0” <http://api.app.com/products>
18+
>
19+
> This approach doesn’t clutter the URI with versioning information.
20+
21+
### Versioning through query parameters
22+
23+
> <http://api.app.com/products?version=1>
24+
>
25+
>Another option for versioning a REST API is to include the version number as a query parameter.
26+
27+
### Versioning through content negotiation
28+
29+
> curl -H “Accept: application/vnd.xm.device+json; version=1” <http://api.app.com/products>
30+
>
31+
>The last strategy we are addressing is versioning through content negotiation.
32+
>
33+
>This approach allows us to version a single resource representation instead of versioning the entire API which gives us a more granular control over versioning. It also creates a smaller footprint in the code base as we don’t have to fork the entire application when creating a new version. Another advantage of this approach is that it doesn’t require implementing URI routing rules introduced by versioning through the URI path.
34+
>
35+
>One of the drawbacks of this approach is that it is less accessible than URI-versioned APIs: Requiring HTTP headers with media types makes it more difficult to test and explore the API using a browser.

0 commit comments

Comments
 (0)