Slide 1

Slide 1 text

Design and development of Web APIs Milton Carranza Senior API Engineer @ MSD [email protected] https://msdit.cz/ 29.10.19

Slide 2

Slide 2 text

Agenda 29.10.19 THE BASIC PRINCIPLES BEHIND THE DESIGN. "BEST PRACTICES" FOR DESIGN AND DEVELOPMENT. API DOCUMENTATION API DESIGN API SECURITY API AUTOMATION

Slide 3

Slide 3 text

Web APIs REST API Design http://www.datascript.cz/en/courses/dev- test/rest-api-design/

Slide 4

Slide 4 text

API Styles of interest 29.10.19 https://speakerdeck.com/zdne/what-api-your-guide-to-api-styles Web APIs REST APIs (Architecture) So called REST APIs Query APIs GraphQL (Framework) SQL (Framework) Publish and Subscribe Kafka (Framework) WebSub (Recomendation) RPC APIs SOAP (Protocol) gRPC (Framework) Flat files

Slide 5

Slide 5 text

Trends over time 10/29/19

Slide 6

Slide 6 text

REST 29.10.19

Slide 7

Slide 7 text

What‘s REST? • Acronym for: Representational State Transfer • Architectural style to create web APIs

Slide 8

Slide 8 text

What‘s REST? • Architectural style to create web APIs • System where documents and other resources are identified by Uniform Resource Locators, linked by hypertext, and accessible over the Internet. • Who is the biggest enabler of this? • Who or what provides the rules to make this happen? 29.10.19

Slide 9

Slide 9 text

• Acronym for: Hyper Text Transfer Protocol

Slide 10

Slide 10 text

• Acronym for: Hyper Text Transfer Protocol • Protocol: set of rules • Transfer: Move from point A to point B • Text: Everything has a text representation… • Hyper-Text: ?

Slide 11

Slide 11 text

What‘s REST? • Architectural style to create web APIs • Acronym for: Active Pharmaceutical Ingredient

Slide 12

Slide 12 text

What‘s REST? • Architectural style to create web APIs • Acronym for: Application Programming Interface • Application: who is intended to consume • Programming: how is intened to be consumed • Interface: boundary between 2 entities • GUI • Graphical: how is intended to be consumed • User: who is inteded to consume • Interface: boundary between a computer and a user • Boundary between 2 entities / systems / modules / class / things • A contract.

Slide 13

Slide 13 text

What‘s REST? • Architectural style to create web APIs • set of constraints that imply a system with certain properties. • It’s the highest level of granularity. 29.10.19

Slide 14

Slide 14 text

REST constraints 29.10.19 Client-server architecture Statelessness Cacheability Layered system Uniform interface Code on demand (optional)

Slide 15

Slide 15 text

Client-server architecture REST Constraints

Slide 16

Slide 16 text

Statelessness REST constraints

Slide 17

Slide 17 text

Cacheability REST constraints

Slide 18

Slide 18 text

Layered system REST constraints

Slide 19

Slide 19 text

Code on demand REST constraints

Slide 20

Slide 20 text

REST API Design Best practices 29.10.19

Slide 21

Slide 21 text

Documenting APIs - API Design First Approach • API Specification • Contract • Several formats • API Blueprint (by Apiary) • Open API Specification – OAS (formerly know as Swagger) • Web Application Description Language – WADL • Restful Service Description Language – RSDL • Restful API Modeling Language – RAML • Benefits of using it • Developer Documentation • Mocks • Automated Testing • SDK Generation • Input Validation

Slide 22

Slide 22 text

Popular REST API Design guidelines Microsoft Google Zalando WSO2 Atlasian Agency for a digital Italy Cisco Heroku PayPal Adidas The White House

Slide 23

Slide 23 text

Common Patterns • Naming conventions • Use Plurals • Use Nouns • Don‘t use Verbs • There are always exceptions! • Good: • /orders, /vehicles, /positions • Bad • /create-order, /order • Exceptions • /v1, /search 29.10.19

Slide 24

Slide 24 text

Common Patterns • Use a Naming convention (only one and stick to it all over your APIs!) • camelCase • UpperCamelCase • snake_case • kebab-case • @ MSD we use kebab-case to name resources and headers, and camelCase to name query parameters and properties. • /my-products, /purchase-orders • /purchase-orders?dateFrom={date} 29.10.19

Slide 25

Slide 25 text

Common Patterns • HTTP Methods for actions • GET, POST, PUT, DELETE, OPTIONS, PATCH, … • HTTP Status Codes • 2XX, 3XX, 4XX, 5XX • Meaningful errors • 4XX, 5XX • Problem for APIs (Standard) • Content Negotiation • JSON by default • Enable Hypermedia • HAL • Siren 29.10.19

Slide 26

Slide 26 text

Common Patterns • Use common data types • Date and Time Format • ISO 8601 - 2019-10-30T09:19Z • Language Code Format • ISO 639 – en, es, zh • Country Code Format • ISO 3166-1 alpha-2 - CZ, SV, ES • Currency Format • ISO 4217 - CZK, USD, EUR 29.10.19

Slide 27

Slide 27 text

Common Patterns • Like scallability? • Use Pagination! • page & pageSize • offset & limit • dateFrom & dateTo • Long running tasks • Language Variants • Use Accept-Language headers • Use the ISO 639 J • Cache • Using Cache-Control header • Version your APIs • URLs, Headers 29.10.19

Slide 28

Slide 28 text

REST API - Security • Use TLS • 1 way TLS - minimum • 2 way TLS – good to have • Keep it Simple, Use a Standard! • Basic Authentication – not recommended • OAuth v2 • Other Mechanisms • API Keys 29.10.19

Slide 29

Slide 29 text

REST API - Security • Use common sense • Never expose information over the URLs - they may and will end up in the logs • Passwords • API Keys • Personal Identifyable Information • Session Tokens 29.10.19

Slide 30

Slide 30 text

REST API - Security • Rate Limiting • Throughput • Protect HTTP Methods • Input validation 29.10.19

Slide 31

Slide 31 text

API Automation • The precondition for any automation are flawless processes! • Design API Specification in a machine readable format • Resources, Methods, Path and Querystring Parameters, input and output Payloads, Authentication mechanisms, etc. 29.10.19

Slide 32

Slide 32 text

API Automation • Now we can automate several details: • Render Developer Documentation • Mocks server implementations for parallel API / Client development • Automated API Testing in your CI/CD pipeline • SDK Generation • Input Validation 29.10.19

Slide 33

Slide 33 text

Takeaways • Understand business problem first, and not privilege tech trends over business goals. • There are plenty of API Styles out there, make sure the one you choose is the right one for your use case • REST is not a silver bullet • Request/response communication model • Return a response as fast as possible • High number of transferred messages • Small payloads • Low latency 29.10.19

Slide 34

Slide 34 text

Takeaways • API as a reusable service • REST is an architecture, there‘s no agreement on how it should be implemented, it‘s about you choosing how you will keep consistency and stick to it all over your API ecosystem • Understand HTTP beyond that CRUD lifecycle, and don’t use something for which HTTP has already solved. 29.10.19

Slide 35

Slide 35 text

Web APIs REST API Design http://www.datascript.cz/en/courses/dev- test/rest-api-design/

Slide 36

Slide 36 text

Design and development of Web APIs Milton Carranza Senior API Engineer @ MSD [email protected] https://msdit.cz/ 29.10.19