RESTful Web API Design
RESTful Web API Design
Rainer Stropek
Rainer Stropek
software architects gmbh
RESTful
Web http://www.timecockpit.com
Mail rainer@timecockpit.com
Twitter @rstropek
Agenda
RESTful Web APIs have become an integral part of modern software packages. They are important for
integration scenarios in enterprises and in the cloud. This workshop is dedicated to designing RESTful
Web APIs. Rainer Stropek, himself founder a SaaS-focused company, will guide you through the world of
RESTful APIs. In particular, Rainer will speak about the following topics:
Short recap of the basic principles of RESTful Web APIs
Real-world RESTful API design (e.g. addressing in multi-tenant systems, versioning, long-running
operations, etc.)
Authentication and authorization with OAuth2 and OpenID Connect
The OData standard for RESTful APIs
The role of metadata using the examples of http://swagger.io/ and OData
Securing and operating RESTful APIs using the example of Azure API Management
Code samples using Node.js with JavaScript and .NET with C#
Attendees of this workshop should have some understanding of http and cloud computing. Practical
experience regarding RESTful API design or development is not necessary.
What is REST?
Representational State Transfer (REST)
HTTP
What is REST?
HTTP
Idempotent requests
What is REST?
HTTP
What is REST?
Important REST principles
Stateless
Cacheable
Layered System
Client cannot tell if connected directly to the server (e.g. reverse proxies)
URIs
Resource representation
Demo
API Design
Design Rules
Do use HTTPS
Design Rules
Do carefully model URI paths
E.g. https://api.myservice.com/customers/ALFKI/orders
Bad example: https://api.myservice.com/afe7f2cb-8e71-4472-a53b-1f8e3712dffc/orders
E.g. https://api.myservice.com/customers/ALFKI/orders
Also for controller resources (e.g. POST for controller that creates data)
Consider firewall problems with PUT and sometimes even DELETE
Avoid using controller names instead of HTTP verbs
Demo
}
};
Custom API
Design Rules
Do use standard response codes as they were intended to
API Design
Location header with POST
Additional error data in
case of 4xx error
Demo
Location header
Design Rules
Dont use GET + query for controller actions that write
Use proper HTTP verbs and parameters in the request body instead
Examples:
https://api.myservice.com/customers?$filter=name eq ALFKI
https://api.myservice.com/customers?$top=10
https://api.myservice.com/customers?$orderby=name
http://petstore.swagger.io/v2/pet/findByStatus?status=sold
Design Rules
Consider support for batching of operations
Design Rules
Consider using Etag and If-None-Match to save bandwidth
Consider using If-Match or If-Unmodified-Since for optimistic
concurrency
Consider allowing to suppress response echo on POST
API Design
Location header with POST
Additional error data in
case of 4xx error
Demo
Design Rules
Do support JSON for resource representation
application/json
E.g. application/xml
API Design
Links for entities in OData
XOData
Demo
Design Rules
Consider configuring CORS to enable broad web API usage
Design Rules
Do limit server resource usage in multi-tenant systems
Examples:
Design Rules
Do plan for versioning your web API
Consider using a custom header for API version to enable complex versioning scenarios
Examples
x-ms-version in Azure Table Storage
OData-MaxVersion and OData-Version headers in Odata
Consider using version-specific URIs for simple versioning scenarios and major versions
Protecting Resources
CORS Cross-Origin Resource Sharing
What is CORS?
XMLHttpReqest limits cross-domain web API calls
Same origin policy: Script can only make HTTP requests to the domain it came from
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
Access-Control headers
Allow-Origin: * or Origin
Allow-Credentials: Cookies included?
Expose-Headers: Non-simple headers available to the client
CORS
Adding CORS support to
ASP.NET Web API
Demo
Protecting Resources
Auth with OAuth2 and OpenID Connect
Local Auth
Enterprise
Servers
Domain
Devices
Everything is Windows
Problems
External devices
External services
Non-Windows environments
OAuth2
Successor of OAuth1 and OAuth WRAP
Standard for delegating authorization for accessing
resources via HTTP(S)
Many different flavors as the standard leaves many decisions up to the developer
Example: https://oauth.io/
Important Terms
OAuth Provider
Resource Provider
Resource Owner
Client
OAuth Endpoints
Authorization Endpoint (aka OAuth-A)
Redirect Endpoint
OAuth Tokens
Authorization Code
Access Token
Refresh Token
OAuth Flows
Authorization Code Flow
Implicit Flow
Less secure
No refresh tokens
For clients that cannot store secrets (e.g. SPA written in JavaScript)
Not compatible
Limited scope
OIC Protocol
OpenID Connect extends
OAuth2
Although rather new, OIC is
already very popular
Libraries and products:
http://openid.net/developers/li
braries/
Source: http://openid.net/connect/
Microsoft Azure
Microsoft Azure
Source:
http://channel9.msdn.com/events/TechEd/Europe/2014/CDP-B210
Open Standards
Why Metadata?
Humans and computers discover and understand services
Examples: BI tools like PowerBI, workflow engines like Azure Logic Apps
Swagger
http://swagger.io
Tools for API creators
Swagger UI (http://petstore.swagger.io/)
Code generators (http://swagger.io/getting-started/swagger-codegen)
Swagger
Swagger editor
Swagger code generator
(AngularJS)
Demo
OASIS standard
Extensible
http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part3-csdl.html
http://www.odata.org/libraries/
https://api.myserver.com/odata/Customers?
$filter=CustomerID eq 15&
$top=10&
$select=FirstName,LastName
http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html
OData
Implementing an OData
service in .NET
OData consumption
XOData
Power BI
Demo
Rainer Stropek
software architects gmbh
Q&A
Mail rainer@timecockpit.com
Web http://www.timecockpit.com
Twitter @rstropek
Try
for free and without any risk. You can get your trial account
at http://www.timecockpit.com. After the trial period you can use
for only 0,25 per user and day without a minimal subscription time and
without a minimal number of users.
Probieren Sie
kostenlos und ohne Risiko einfach aus. Einen
Testzugang erhalten Sie unter http://www.timecockpit.com. Danach nutzen
Sie
um nur 0,25 pro Benutzer und Tag ohne Mindestdauer
und ohne Mindestbenutzeranzahl.