Summary
A new pico lesson is available that shows how to use user-defined actions in modules to wrap an API.
I recently added a new lesson to the Pico Programming Lessons on Modules and External APIs. KRL (the pico programming language) has parameterized modules that are great for incorporating external APIs into a pico-based system.
This lesson shows how to define actions that wrap API requests, put them in a module that can be used from other rulesets, and manage the API keys. The example (code here) uses the Twilio API to create a send_sms()
action. Of course, you can also use functions to wrap API requests where appropriate (see the Actions and Functions section of the lesson for more detail on this).
KRL includes a key
pragma in the meta block for declaring keys. The recommended way to use it is to create a module just to hold keys. This has several advantages:
- The API module (Twilio in this case) can be distributed and used without worrying about key exposure.
- The API module can be used with different keys depending on who is using it and for what.
- The keys module can be customized for a given purpose. A given use will likely include keys for multiple modules being used in a given system.
- The pico engine can manage keys internally so the programmer doesn't have to worry (as much) about key security.
- The key module can be loaded from a file or password-protected URL to avoid key loss.
The combination of built-in key management and parameterized modules is a powerful abstraction that makes it easy to build easy-to-use KRL SDKs for APIs.
Going Further
The pico lessons have all been recently updated to use the new pico engine. If you're interested in learning about reactive programming and the actor model with picos, walk through the Quickstart and then dive into the lessons.
Photo Credit: Blue Marble Geometry from Eric Hartwell (CC BY-NC-SA 3.0)