Slide 1

Slide 1 text

Building APIs that DON'T SUCK !

Slide 2

Slide 2 text

Neo Ighodaro CTO, Hotels.ng Ambassador, Auth0 Technical Writer, Pusher Twitter/GitHub: @neoighodaro Website: neoighodaro.com

Slide 3

Slide 3 text

What we will be considering

Slide 4

Slide 4 text

4 Database – Planning Structure and Content 4 Endpoints – Planning and Creating 4 Responses 4 HTTP Status Codes, Errors and Messages 4 Data Output Structure 4 Security and Controlled Access 4 Other Considerations

Slide 5

Slide 5 text

Database Planning Structure and Content

Slide 6

Slide 6 text

Database Tables & Seeding Your tables should reflect the resources you wish to have and it is very dependent on the features of your application. For instance, building an Android book store might have resources like: users, books, book_categories, book_downloads etc.

Slide 7

Slide 7 text

Database Tables & Seeding Seeding is basically adding mock data to your database so you can run tests and have data returned. Seeds are NOT meant to be used in production. You can use libraries that generate fake data to create fake data for each column in your database table.

Slide 8

Slide 8 text

Endpoints Planning and Creating Endoints

Slide 9

Slide 9 text

Functional Requirements Think through everything your API will need to handle. This usually starts out with the resources and the CRUD (Create, Read, Update, Delete) operations for them. List each resource out (a resource can be "books" or "places" or "articles")

Slide 10

Slide 10 text

Functional Requirements Gradually dive deeper into each resource and see if it needs additional methods or additional parameters. For instance, you might have a "books" resource and you want a cool feature like "Favorite Book". Those things need to be planned also.

Slide 11

Slide 11 text

Posts Categories Users Create Create Create Read Read Read Update Update Update Delete Image Image

Slide 12

Slide 12 text

Creating Endpoint From Resources You can turn the resources you created to generate endpoints that respond to different HTTP verbs. You will need a little knowledge on RESTful APIs and “best practices” for naming conventions. hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9

Slide 13

Slide 13 text

! Useful Tip! Use GET for read operations, POST for create operations, DELETE for delete operations and PUT for update operations. hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9

Slide 14

Slide 14 text

Responses HTTP Status Codes, Errors and Messages

Slide 15

Slide 15 text

HTTP Status Codes Your HTTP status code should be proper and match what you are trying to tell the client. For instance, when a resource is not found, a 404 - Not Found status code must be passed to the client. When a resource is created, you can pass a 201 - Created. https://restfulapi.net/http-status-codes/

Slide 16

Slide 16 text

Errors and Messages Apart from sending the right HTTP status code, your errors and success messages should be descriptive enough to know what is going on. https://restfulapi.net/http-status-codes/

Slide 17

Slide 17 text

Responses Data Output Structure

Slide 18

Slide 18 text

{ "data": { "id": 123456 "name": "Neo Ighodaro", "comments": { "data": [ { "id": 1234 "text": "The tin go skraaaa..." }, { "id": 1234 "text": "Pa pa ka ka ka..." } ] } } } http://jsonapi.org/format/ By using a standard, you will always have consistent response across endpoints. No surprises.

Slide 19

Slide 19 text

Securing your API from the bahd guys

Slide 20

Slide 20 text

API Authentication https://auth0.com/docs/api-auth

Slide 21

Slide 21 text

Working with scopes http://docs.apigee.com/api-services/content/working-scopes

Slide 22

Slide 22 text

Other Considerations Always Write Unit Tests Return error messages as codes e.g BOOK_NOT_FOUND Document API with tools like Swagger.io Employ Caching with tools like Varnish

Slide 23

Slide 23 text

Thank You The slides go SKRRRRAAAAA! Slide is available at https://bit.ly/building-apis-that-dont-suck