API Design On The Scale of Decades v2.2
API Design On The Scale of Decades v2.2
Decades
Learn How to Architect and Design
Long-lasting APIs
Nordic APIs
© 2016 - 2022 Nordic APIs
Tweet This Book!
Please help Nordic APIs by spreading the word about
this book on Twitter!
The suggested tweet for this book is:
Check out ”API Design on the Scale of Decades” - the
latest eBook by the @NordicAPIs team
The suggested hashtag for this book is #APIdesign.
Find out what other people are saying about the book by
clicking on this link to search for this hashtag on Twitter:
#APIdesign
Also By Nordic APIs
Developing the API Mindset
The API Lifecycle
Securing The API Stronghold
API-Driven DevOps
The API Economy
Programming APIs with the Spark Web Framework
How to Successfully Market an API
GraphQL or Bust
API Strategy for Open Banking
Identity And APIs
API as a Product
Developer Experience
This book is dedicated to the speakers, attendees, and
sponsors that continually make Nordic APIs events
wonderful!
Contents
Supported by Curity . . . . . . . . . . . . . . . . . . i
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
APIs on the Scale of Decades . . . . . . . . . . . . . iii
The API of Me . . . . . . . . . . . . . . . . . . . . . . . . 91
Confidential . . . . . . . . . . . . . . . . . . . . . . . 92
Financial . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Tactile . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . 96
Final Thoughts . . . . . . . . . . . . . . . . . . . . . . 97
In Summary . . . . . . . . . . . . . . . . . . . . . . . . . 132
TL/DR - 15 Important Takeaways . . . . . . . . . . 132
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . 134
Endnotes . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
CONTENTS i
Supported by Curity
curity.io.
Preface
APIs on the Scale of Decades
Though GET and DELETE coordinate well, POST, PUT, and PATCH
aren’t directly synonymous with CRUD operations. For
example, POST doesn’t necessarily only mean “Create”.
It’s actually a very versatile method — so versatile that
the entire SOAP protocol is tunneled through the POST
method when used with HTTP.
Since HTTP methods don’t map cleanly to CRUD, Uls-
berg argues that API providers should consider how they
might describe their APIs in a different way:
http://api.nordicapis.com/authors/contributor?author=doerrfeld
http://api.nordicapis.com/blogpost/getPostById?id=47
http://api.nordicapis.com/blogpost/47/edit-form
http://api.nordicapis.com/blogpost/47
http://api.nordicapis.com/128ndoels-8asdf-12d5-39d3
“DON’T”
1. Client-Server
2. Stateless
3. Cacheable
4. Layered
5. Code on demand (optional)
6. Uniform Interface
• Identification of resources
• Manipulation of resources
• Self-descriptive messages
• Hypermedia as the engine of application state
(HATEOAS)
HTTP/1.1 200 OK
{
"Id": "/toaster",
"state": "off",
"operations": [{
"rel": "on",
"method": "PUT",
"href": "/toaster",
"Expects": { "state": "on"}
}]
}
HTTP/1.1 200 OK
{
"Id": "/toaster",
"state": "on",
"strength": 0,
"operations": [ {
"rel": "PUT",
"method": "PUT",
"href": "/toaster",
"expects": { "state": "off" }
}, {
"rel": "strength",
Designing a True REST State Machine 11
"method": "PUT",
"href": /fcesj48fl29304d827434j
"expects": {
"strength": [1,2,3,4,5,6]
}
}]
}
HTTP/1.1 200 OK
{
"Id": "/toaster",
"state": "heating",
"strength": 3,
"operations": [ {
"rel": "PUT",
"method": "PUT",
"href": "/toaster",
"expects": { "state": "off" }
}, {
"rel": "strength",
"method": "PUT",
"href": /fcesj48fl29304d827434j
"expects": {
"strength": [1,2,3,4,5,6]
}
}]
}
Designing a True REST State Machine 12
HTTP/1.1 200 OK
{
"Id": "/toaster",
"state": "idle",
"strength": 3,
"operations": [ {
"rel": "PUT",
"method": "PUT",
"href": "/toaster",
"expects": { "state": "off" }
}, {
"rel": "strength",
"method": "PUT",
"href": /fcesj48fl29304d827434j
"expects": {
"strength": [1,2,3,4,5,6]
}
}]
}
Conclusion
Over/Under Fetching
REST suffers from the fact that it’s often used for some-
thing it wasn’t really designed for, and as a result, it often
must be heavily modified. That’s not to say that REST
doesn’t have its place — it’s only to say that it may not
be the best solution for serving client applications. As
Facebook says in its own documentation:
Now that we’ve seen the issues with REST, how, exactly,
does GraphQL solve them?
Conclusion
Recently, there has been debate over what the best prac-
tices are for versioning an API. Many public web APIs are
retired as new versions replace them, but if you were to
ask Roy Fielding, creator of REST, he may tell you not to
version your API at all.
Some companies are taking matters into their own hands,
and seeking out innovative ways to handle the cumber-
some process of keeping their Application Programming
Interfaces up to date in a way that makes sense to their
business model. These new strategies place more empha-
sis on evolution rather than deprecation.
The typical v1, v2, v3 etc. versioning approach focuses
on releasing large sweeping updates to improve the API
experience. But the downside of this method is that it
causes a major breaking change on the client side. For
internal API-first companies that have granular control
over their various web, desktop, and mobile clients, con-
tinuous versioning could be a more attractive strategy.
In this chapter, we’ll review how public web APIs are
typically versioned within our domain, and discuss why
companies may want to consider a continuous versioning
strategy for handling complex APIs that are subject to
Continuous Versioning Strategy for Internal APIs 24
Additional Resources
APIs have been around for a long time. While that means
there’s a lot of great tools from a lot of amazing develop-
ers, it also means that, as a community, the API space has
held on to some practices for a long time – some would
argue too long in many cases.
One such holdover, according to some developers, is the
concept of polling. While polling itself is not a bad thing
– after all, it’s a simple implementation of an endpoint
call – many argue the effects of constant polling require
a solution, and an immediate one at that.
Enter REST Hooks. Today, we’re going to take a look at
the concept and application of REST Hooks, and exactly
why some argue for their necessity. We’ll discuss some
objections to the idea of polling, and the responses from
their supporters.
Counter Arguments
Create a Subscription
POST <subscribe_endpoint> \
-H Authenticated: authenticationSolution \
-H Content-Type: application/json \
-d ‘{“target_url”: “https://hooks.zapier.com/hooktest>”,
“Event”: “user_created”}’
• Authenticated User
• target_url
• event
Putting an End to API Polling 55
Sending Hooks
Now that the hook is setup, we need a way for the API to
actually send the data. We can do this by implementing
the following POST code:
POST https://hooks.zapier.com/testhook \
-H Content-Type: application/json \
-d <json payload>
DELETE <unsubscribe_endpoint> \
-H Authenticated: authenticationSolution \
-H Content-Type: application/json \
Alternatives
Conclusion
Imagine that a user has plugged into your API and triggers
a request, only to be met with an error message. Now
imagine all of this is happening while they’re traveling
at 70mph on the highway. For Henrik Segesten, cloud
domain architect at Volvo, making sure that this sort of
thing doesn’t happen isn’t the stuff of bad dreams — it’s
part of his day to day routine.
Segesten and co. can’t afford to make the same mistakes
that API developers working on a plugin for, say the
Nest thermostat can. Rather than cause a minor incon-
venience for the user, the potential impact of flaws in an
automobile scenario could be life-threatening. From the
transmission to the software powering onboard electron-
Is Your API Automotive Grade? 68
• JSON
• REST
• SOAP
• XML
• JavaScript
• Open API/Swagger
• GraphQL
Final Thoughts
The good news is that, for the majority of us, our API
consumers will never be using our products or services
at 70mph. But that doesn’t mean that it’s not a good idea
to prepare like they might be…
Why OAuth 2.0 Is Vital to
IoT Security
Proof of Possession
authorization_code = XYZ
Client_id = device123
Client_secret = supersecret
Scope = read_ekg
Audience = ekg_device_ABC
authorization _code = XYZ
…
Key = a_shortlived_key
Access_token = oddfbmd-dnndjv…
This is the final step — the client is now fully and truly
authenticated. With this acess_token, the client can start
a session on the IoT device. The IoT device will look at
this access_token, and pass it to the Authorization Server (if
it’s a connected device) asking for verification to trust the
device. When the Authorization Server accepts the verifi-
cation, it passes a new key to the IoT device, which then
returns it to the Client, establishing a trusted connection.
Why OAuth 2.0 Is Vital to IoT Security 80
Disconnected Flow
Client_id = ekg_device_ABC
Client_secret = supersecret
Scope = read_result
Audience = connected_tbie_123
Token = original_token
...…
Key = a_shortlived_key
Conclusion
the rate of change for your data is. For example, The
average rate of change for Typeforms are typically 1-2
weeks. Identifying a pattern for the rate of change in your
data can help you design your webhooks to be as lean as
possible.
Harmon recognizes one catch to this approach — he rec-
ommends still using a polling API alongside the webhook
payload to carry out sporadic system checks and perform
large downloads.
4: Generic actions
/resource/:id/generic-name + {action:process}
Confidential
Financial
Tactile
Aggregate
personal data.
Final Thoughts
That sure sounds like the last time your site went down
and every team in the office started panicking, right?
When things go wrong, everybody starts pointing fingers.
“The developers are saying it’s a server issue, IT is saying
Avoid Walking on Eggshells and Use DevOps 99
2. Share information
3. Consistency
4. Increased accountability
5. Embrace failure
API Fastness
Any good engineer can tell you that structures don’t fail
because of the last brick, they fail because of the first. If
you start with a shaky foundation, or bad bedrock, you’re
going to collapse, and the same is true of an API.
5 Ways the OpenAPI Specification Spurs API Agility 120
Some of this may feel like overkill for startups and smaller
businesses, but APIs are like Pringles – once you pop, you
just can’t stop. Salesforce, for example, has 10 APIs and
Microsoft Cognitive Services have 23. Google has such
a large suite of APIs that they have an API explorer to
navigate them. And, with companies increasingly using
freemium or paid API models, the trend of multiple APIs
and microservices will only become more prevalent.
Wang highlights the way in which the size of a company
doesn’t always correlate with the complexity of an organi-
zation anyway: “Even small companies have very complex
authorization structures in roles sometimes, depending
on what kind of domain they’re working in, and it’s really
important to have a platform that supports different
roles, additive roles etc.” Even smaller companies need to
think about all of this, sooner rather than later, if they’re
thinking about building a suite of APIs.
Case Study: Bosch Ongoing Enterprise API Management Saga 131
What’s Next?
Stay Connected