Top Banner
l All contents Copyright © 2014, MuleSoft Inc. Building Your API for Longevity by mike stowe
80

Building Your API for Longevity

Jul 09, 2015

Download

Technology

MuleSoft Inc.

This is not about how to code your API, but rather to show you what steps and best practices you need to utilize to build a successful, long-lived API. Thankfully with 5 Simple Steps you can build an API that is designed to last.

1 – Go in with a long-term mindset
2 – Understand what you’re building
3 – Utilize Spec Driven Development
4 – Incorporate Best Practices
5 – Repeat steps 1-4 for all new features
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Building Your API for Longevityby mike stowe

Page 2: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

This talk is not about how to code your

API, but rather to show you what steps and

best practices you need to utilize to build

a successful, long-lived API to the extent

that we can in 45 minutes.

Disclaimer

Page 3: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

• API Fanatic

• Open Source Contributor

• Author, Speaker, Consultant

• 10+ years hacking Professional Code

• Dev Relations Manager at MuleSoft

About Me

Page 4: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

APIs are changing the world.

Page 5: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Over 13,000 PUBLIC APIs

Page 6: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Page 7: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Today APIs are connecting…

Page 8: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

PHONES

Page 9: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

WATCHES

Page 10: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

GLASSES

Page 11: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

CARS

Page 12: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

REFRIGERATORS

Page 13: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

THERMOSTATS

Page 14: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

IN HOME ROBOTS

Page 15: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

AND MORE

Page 16: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

THINK ABOUT THAT…

Page 17: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

In order to work, the IoT requires that APIs remain persistent.

Page 18: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

+

Page 19: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Versioning and making changes is expensive…

Page 20: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

FOR EVERYONE.

Page 21: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Thankfully with 5 Simple Steps you can build an API that is designed to last.

Page 22: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

1 – Go in with a long-term mindset

2 – Understand what you’re building

3 – Utilize Spec Driven Development

4 – Incorporate Best Practices

5 – Repeat steps 1-4 for all new features

They are:

Page 23: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

• Your API is a Contract

• Versioning is not a solution

• Understand you suck at design

• You can pay a little now, or much more later

• You need to think things through

• Mind-set is everything

Think Long-term:

Page 24: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 24

Your API is a contract, it’s your word to your users. Users who are not only depending on a working API to integrate with your service, but in order to provide food for their families.

Your Users are Depending on You…

Page 25: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

This means you need to think through every aspect of your API before building it.

Page 26: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 26

•  Who is your API for?

•  What type of API are you building?

•  How are you going to maintain your API?

•  How are you going to document your API?

•  How are you going to let users interact with your API?

•  How are you going to manage authentication,

provisioning, throttling, and developer security?

•  How are you going to protect your servers against

attacks, developer misuse, etc?

•  How are you going to manage support?

Thinking things through…

Page 27: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 27

•  Who are your end users?

-  Current customers

-  Business partners

-  Third-party services

•  What actions do they need access to?

-  This means sitting down and talking to them!

•  How can you involve them in the design process?

-  Your users should be involved from Day One.

Who Will Be Using Your API?

Page 28: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 28

•  List out WHY you are making the API

-  Saying that you’re exposing your data to users is not good enough- explain HOW they will use it

•  Explain how your API will interact with existing services

•  List out the actions the API needs to be able to handle

-  Users: add, edit, reset password, delete, etc…

-  Messages: draft, send, retrieve, archive, etc…

•  Do only what is NECESSARY

•  DON’T get fancy.

What is the Purpose of Your API?

Page 29: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 29

List Out What Your Users Need to be able to Do:

Page 30: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 30

•  Are you building a REST, partial REST, SOAP, or RPC

based API?

•  Why are you building your API in that format?

•  What does that mean for development?

•  What does that mean for usability?

•  What does that mean for longevity?

What Type of API Are You Building?

Page 31: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 31

What Type of API Are You Building?

Page 32: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 32

•  Client-Server

•  Stateless

•  Cacheable

•  Interface/ Uniform Contract

•  Layered System

•  Code on Demand (optional)

Do You Understand the REST Constraints?

Page 33: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Most APIs are NOT RESTful.

Page 34: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

This is why it’s so important to understand the different types of APIs, why each type is different, and why you are choosing one over the other.

Page 35: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

It also means building your API for beyond today…

Page 36: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 36

...people are fairly good at short-term design, and usually awful at long-term design.”

“-Dr. Roy Fielding

Page 37: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Versioning is a necessary evil.

Page 38: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 38

•  Backwards incompatibilities

•  Multiple Services to Maintain

•  Multiple Systems to Support

•  Creates confusion among developers

•  Developer adoption is nearly impossible

Problems with Versioning

Page 39: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 39

•  Backwards incompatible platform changes

•  Your API is no longer extendable

•  Your spec is out dated (ie SOAP)

You Need to Version When:

Page 40: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 40

•  Added new endpoints

•  Added additional data in response

•  Changed technologies (java toruby)

•  Changed your application’s services (code)

But You Shouldn’t Version Just Because You:

Page 41: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Versioning does not excuse poor design.

Page 42: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

And a poorly designed API will cost you far more in the long run, adding months to fix what could have been prevented in weeks. There are no shortcuts or quick fixes, you can either build your API the right way to begin with, or pay substantially for it in the long-run.

Page 43: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

• Define your API before Coding

• Use Design Patterns/ Code Reuse

• Mock and get User Feedback

• Make Necessary Changes

• Start Coding – to the Spec

• DO NOT DEVIATE!

Use Spec Driven Development

Page 44: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Spec Driven Development means a hybrid between agile and waterfall methodologies. You should develop your spec iteratively, incorporating agile user testing. However, the actual development (coding) of your API should be static, driven by the spec with no deviation.

Page 45: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Disclaimer: Waterfall refers to the spec and changing the spec only! You should still use sprints for code development – just at this point the spec should not be changing.

Page 46: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 46

Hybrid Approach

Design Development

Continuous, fluid, changeable Static… No turns

Page 47: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 47

The Agile Design Cycle

Page 48: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

The goal is that by utilizing agile user testing, carefully designing, and prototyping your API in an iterative state, that most design related issues have already been discovered and resolved. Allowing you to develop fearlessly.

Page 49: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

The problem is up until now, designing and prototyping an API has been extremely costly. Requiring developers to create a mock API through extensive coding, and without any real constraints/ pattern reuse.

Page 50: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

However, there are several new specs being driven by today’s industry leaders making it easier define your API: with tools to design, prototype, document, and allow user interaction.

Page 51: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 51

•  RAML

•  IO Docs

•  Swagger

•  API Blueprint

Some of Today’s Specs:

Page 52: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 52

•  You can define your API in just a few lines of code

•  You can see what it would look like as you go

•  You can quickly prototype it for devs to try

•  You can quickly make tweaks/ changes

•  You can easily document your API

•  You can let developers try your API online

•  You can let developers interact with your and other APIs

•  Generate SDKs/ client libraries for your API (APIMatic.io)

Using RAML You Can:

Page 53: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 53

•  You can use design patterns

•  You can reuse code (traits, resourceTypes)

More Importantly…

resourceTypes:      -­‐  collection:              description:  Collection  of  available  <<resourcePathName>>  in  Jukebox.              get:                  description:  Get  a  list  of  <<resourcePathName>>.                  responses:                      200:                          body:                              application/json:                                  example:  |                                      <<exampleCollection>>                

Page 54: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 54

The RAML API Designer

Page 55: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 55

What Does RAML Look Like?

#%RAML  0.8      title:  World  Music  API  baseUri:  http://example.api.com/{version}  version:  v1  /playlists:      get:          responses:              200:                  body:                      application/json:                              example:  |                              {                                    “playlistID”  :  1,                                                “playlistName”  :  “My  Awesome  Playlist”,                                          “genre”                :  “top40”,                                  “songs”                :  40                              }            

Page 56: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Remember, your spec is not a one-and-done, rather it is the blueprint for your API. Anytime you do something to your API you should be modifying the spec and going through user testing before writing code. You should never have code that does something not defined by your spec.

Page 57: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

• Use Nouns

• Use CRUD

• Use Hypermedia (HATEOAS)

• Use Accept/ Content-Type

• Return Header Codes

• Return Descriptive Error Messages

Incorporate Best Practices:

Page 58: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use Nouns.

Page 59: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use:

/users    Not:

/createUser    /getUser  /deleteUser  

Page 60: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Utilize CRUD.

Page 61: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Create (POST)  

Read (GET)    

Update (PUT/  PATCH)  

Delete (DELETE)  

Page 62: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use Hypermedia.HATEOAS

Page 63: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 63

•  HAL

•  JSON-LD

•  JSON API

•  Siren

Most Popular Hypertext Link Specs

Page 64: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

{      "data"  :  {          "user":  {              "fname":"first",              "lname":"last"          }      },        "_links"  :  {          "edit":  {              "href"  :  "/api/user/id/1"          },          "message":  {              "href"  :  "/api/message/id/1/lname/last"          }      }  }  

HAL

Page 65: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use Accept/ Content-Type Headers.

Page 66: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc. 66

Using headers gives you flexibility to support multiple

types of formats from the same resource without

worrying about breaking backwards compatibility.

Most common:

•  application/json - wider language support

•  application/xml

Use Accept/ Content-Type Headers

Page 67: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use Response Codes.

Page 68: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

•  200 – OK•  201 – Created•  304 – Not modified•  400 – Bad Request•  401 – Not Authorized•  403 – Forbidden•  404 – Page/ Resource Not Found•  405 – Method Not Allowed•  415 – Unsupported Media Type•  500 – Internal Server Error

Page 69: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

•  418 – I’m a Teapot…•  420 – Enhance Your Calm

Or if you’re feeling super creative…

Page 70: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Use Descriptive Error Messages.

Page 71: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

{      'exception'  {          'code'  :  'e3526',          'message'  :  'Missing  UserID',          'description'  :  'A  UserID  is  required  to  edit  a  user.',          'link'  :  'http://docs.mysite.com/errors/e3526/'      }  }  

The more information you provide, the easier it will be for developers to integrate your API without contacting Support.

Page 72: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Also be sure to keep your documentation up to date and simple enough for developers to quickly find and implement solutions to even the most complex problems. Poor documentation has been the death of many an API.

Page 73: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Finally, when adding new things to your API, be sure to:

Page 74: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

1 – Go in with a long-term mindset

2 – Understand what you’re building

3 – Utilize Spec Driven Development

4 – Incorporate Best Practices

Finally, when adding new things to your API, be sure to:

Page 75: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

It only takes ONE little thing to significantly reduce your API’s life span. Every action you make on your API must be carefully thought out and tested BEFORE being pushed to production.

Page 76: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Remember...

Page 77: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Building an API is easy.

Page 78: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

Designing an API is hard.

Page 79: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

read more @blogs.mulesoft.org

Page 80: Building Your API for Longevity

l All contents Copyright © 2014, MuleSoft Inc.

@MuleDevwww.mulesoft.com