Building Better APIs with Play!

Tuesday, March 4, 2014
Joe Conley

At Point.io, we’re in the business of building APIs to power our enterprise solutions. We’ve invested a lot of time and energy choosing a suitable framework to serve these APIs, and we’ve concluded that the Play Framework (particularly for Scala) affords us the performance, scalability, and development ease-of-use to meet our needs.

RESTful Architecture

We believe it’s important to build our APIs in a RESTful manner. Play is inherently stateless, not storing any information on the server. This not only conforms to RESTful principles but facilitates vertical scaling (several instances of Play on one server) and horizontal scaling (several servers running Play). Play also leverages a cache to allow clients to cache responses. This includes not just simple resources but complex server-side actions as well. Furthermore, building routes for RESTful resources in Play is very straightforward, and also has the added benefit of static compilation which ensures that invalid routes will be caught at compile time.

Authentication and Security

While there’s no out-of-the-box solution for authentication in Play, there is sufficient documentation regarding Action composition, which highlights a very basic authentication example. Action composition allows us to assign multiple behaviors to specific actions. For example, we built granular actions to handle authorization (is this authenticated user authorized to access this resource) and performance monitoring (how long did this action take), then composed them to enforce both behaviors as needed. In addition to these lower-level concerns, Play offers Filters to handle customization of cross-cutting concerns like content encoding and logging.

JSON Reads/Writes

JSON is arguably the most popular data-interchange format for APIs and back-end services. Play Scala offers an excellent library for reading and writing JSON. It offers great functionality for both lower-level operations as well as mapping and validating complex JSON structures. It also leverages Scala Macros to allow for concise reading/writing with Scala case classes.

You can find code examples for each of these topics here

Conclusion

We’ve been able to build powerful, scalable APIs with minimal cost using the Play Framework. We’re pleased with the outcomes thus far and are looking forward to leveraging more of the Typesafe stack to enhance our technology offerings