An API is a set of routines or functions. It’s an interface used for performing tasks, retrieving data, and manipulating data. APIs use HTTP methods, which are used on a resource to GET, POST (submit form data), PUT (for updating files), PATCH (a partial update), and DELETE.
Adding an API
- Bolt-on strategy – when you already have an application and are adding an API to it
- Greenfield strategy – no underlining application. There’s complete freedom and flexibility to do what you want. Generally the “API first” or “mobile first” mentality.
- Facade strategy – wraps existing logic with replace as you go. This is ideal for legacy systems so the application is always functional.
Designing the Relationship
- Independent – the resources may exist regardless of the other existing but they may reference each other
- Dependent – one resource cannot exist without the parent
- Associative – they are independent of each other but the relationship contains additional properties to describe it
Planning the Relationships
- Ask yourself the following questions:
- Can both resources exist without the other?
- Does one resource only exist when the other exists?
- Does the relationship between resources require more information than just the links between them?
- It should be relatively easy to map out the flow, action, etc for each resource. If not, you need to revise your design.
- Consider using notecards to write down each resource action
REST
- REST stands for Representational State Transfer
- It’s an architecture for designing network-based applications
- It is not a protocol, framework, or standard
Benefits of Stateless Servers
- Visibility – monitoring systems and developers do not need to look beyond the request to trace a bug
- Reliability – easy to recover from system failures
Drawbacks of Stateless Servers
- Network Bandwidth – client sends state for every request
- Complexity – all clients must handle their states
Benefits of Caching
- Performance – for stateless and caching, many requests do not need to go all the way to the server
- Scalability – server gets fewer requests so it can handle more clients
Drawbacks of Caching
- Data reliability – clients might use stale data
Facets of a Uniform Interface
- Self descriptive messages
- Server includes metadata, such as Content-Type, to help clients process the responses
- Hypermedia as the engine of application state (HATEOAS)
- Client only assumes a fixed entry-point to the API, the server tells clients all other available actions through hyperlinksBenefits of a Layered System
- Encapsulation (simplify an interface to a legacy server)
- Scalability (layers enable load balancing)
- Security (add access control rules to data crossing a boundary, just like a firewall)
Drawbacks of a Layered System
- Latency – adding layers increases latency