Dzone Refcard 303 Api Integration Patterns 2020
Dzone Refcard 303 Api Integration Patterns 2020
API Integration
CONTENTS
∙ Introduction
∙ API Integration Practices
∙ Integration Practice Matrix
∙ API Integration Patterns
We all hear it so often that we almost stop hearing it: “Integration instance of systems or applications you control — vs. digital
is critical to meeting users’ needs.” products, which offer integration as a feature or service in support
of products’ value propositions.
Integration work consumes 50%-80% of the time and budget of
digital transformation projects, or building a digital platform, while Beyond that, the explosion of applications used today means
innovation gets only the leftovers, according to SAP and Salesforce. there’s a ton of integrations to build, so think about the integration
And as everyone from legacy enterprises to SaaS startups launches experience you need to offer or who will do the building. Will it be
new digital products, they all hit a point at which the product centralized (in IT) and/or productized (for self-serve use), or will it
cannot unlock more value for users or continue to grow without be user-driven (usually for ad hoc integration needs). With these you
making integration a feature. can create a simple two-by-two matrix (see Figure 1 on page three).
If I were to sum up the one question behind all of the other questions As they say, there’s a right tool for every job — in this case, an
that I hear from customers, enterprises, partners, and developers, integration platform or iPaaS. Let’s consider some common
it would be something like: “Is integration a differentiator that we integration use cases, and then look at the pros and cons of these
should own? Or an undifferentiated but necessary feature that options.
supports what we’re trying to accomplish?”
This Refcard won’t try to answer that question for you. Rather, no
matter what type of development work you do, API integration is a
fact of life today, like gravity. Why? Today, experience is paramount.
The average enterprise uses more than 1,500 cloud applications
(with the number growing by 20% each year). Every app needs to
integrate with other systems in a fluid and ever-changing application
ecosystem. So instead, I’ll share some of the common practices
you’re likely to contend with as well as some patterns to consider.
1
REFCARD | API INTEGRATION PRACTICES AND PATTERNS
Coupa? You likely have a legacy enterprise service bus (ESB) or other Let’s say that your company has, or is building, custom applications
middleware running on-prem and integrated with the ERP, but it will and needs to integrate them with other cloud (or on-prem)
take a lot of custom code to work with Tipalti’s API. applications and data. No iPaaS will offer an API connector to your
custom app out of the box, which means you’ll have to write and
If Tipalti offers the embedded integration for your ERP, problem maintain custom code, perhaps within a legacy iPaaS.
solved. Otherwise, look for an API-accessible, pre-built connector like
those from SAP Open Connectors or Axway’s Integration Builder.
Instead, look for an extensible integration platform that allows you Table 1: Error code descriptions
to create reusable, first-class API connectors that others (developers,
CODE ERROR AND DESCRIPTION
ad hoc integrators, etc.) across the company could take advantage
of to deliver their own integrations and take pressure off the 1XX Informational 302 Temp. Found 410 Gone
API INTEGRATION PATTERNS 3XX Redirection 304 Not Modified 412 Precondition
Integration is more complex than it looks. It’s often said that
connecting to an API is easy, but secure integrations that deliver 4XX Client Error 307 Temp. Redirect 413 Entity Too Big
• Every API is unique – Like snowflakes, 200 Executed 401 Unauthorized 415 No Media Type
When receiving API keys, it’s best to copy and store them in a
password manager and treat them like any other password.
Figure 2: OAuth workflow Additionally, it is the preferred pattern since the implementation is
just a URL, as opposed to polling where you need to create a polling
framework that manages the frequency and scope of the information
you wish to receive. For example, Marketo’s polling framework is 239
lines of code compared to just five lines for a similar webhook.
EVENTING We’ve automated the movement of data so that our integration is live
We’ve navigated documentation, authenticated access, and can /GET and fluid, but now it’s flowing in like a river when all we really need
a “Hello World” response — now, let’s bring the data to life, or at least is a small piece of the data that is useful to what our application is
automate it. In some cases, eventing isn’t needed and you can skip doing. We need to set query parameters to filter out all of the data
ahead. However, the true power of integration is the ability to put that we don’t need. Querying is the last part of the API endpoint, or
data in motion without the bottleneck of human clicks. There are two path, that directs the call to pull only the data you want:
POLLING
The ability to search against databases as well as the target
Polling is very much like the kid sitting in the backseat on a road trip
applications is a crucial step. This allows you to test and confirm
constantly asking, “Are we there yet?” But in our case, computers
responses and see any difference in the data structure between what
don’t get annoyed, so the conversation goes like this:
is returned and what is in the documentation. Querying allows you to
“Any changes in data yet?” modify the key-value pairs of the request. At the end of the URL path,
“No, no, no, no, yes, no, no, yes, and so on.”
the query starts with a question mark (?) and is separated with an
To detect if there have been any changes in the data — create, ampersand (&). For example:
retrieve, and delete events, specifically — polling is sending requests
GET /widgets?type=best&date=yesterday
at a predetermined frequency and waiting for the endpoint to
respond. If there is no response body, then there have been no
To test an endpoint, there are multiple options depending on which
changes. Because of this continual request for information, polling
language you prefer, but the majority of API documentation will
is very inefficient and can be expensive when paying for computing
reference commands in curl. “cURL” stands for Client URL and
time, as the vast majority of requests go unanswered.
is a command line tool that becomes particularly valuable when
Finally, because of the intervals in timing, polling is immediately retrieving data from other applications. To test an API, open up your
out of date once the API call returns, batching any new updates that Terminal and copy/paste the curl command to see what response
would have happened since the last call. you get back. Simple enough, right?
Now try adjusting the key-value pairs and fine tune to just the FIXED DATA PAGES
information your application needs from the target endpoint. When Fairly straightforward, when adding into the query, you select
adding parameters to a curl command directly, be sure to add a which page of data you would like returned. For example, to
backslash (/) before the question mark of the query as ? and & return the fourth page of results, you would use this query: GET /
are special characters. Tweaking these pairs will decrease the size widgets?page=4. This method is preferred if the application you
and overhead your application might expect. Simplicity is the act are integrating with has known pages and you would like the data
of demystifying something without losing the magic, and the same returned sequentially. However, it becomes harder if you aren't sure
could be said for APIs. You can always increase the amount of data — what exactly is on that fourth page.
especially in testing — to see where you might run into errors.
FLEXIBLE DATA PAGES
SORTING AND ORDERING Similar to fixed data pages, you could still call the fourth page
An important piece to getting the information you need is how the of widgets, but now you can specify the size of the page. Calling
data will be presented at first glance, which becomes especially GET /widgets?page=4&page_size=25 allows you to further
helpful in testing and presenting data through a UI. Many APIs will dictate what you will get back in terms of page size. This can be very
support sort, sort_by, or order parameters in the URL to change helpful if you're building a UI and need the results to be a certain size.
the ascending or descending nature of the data you’re working with.
For example, a GET /widgets?sort_by=desc(created) can give BULK
us the freshest widgets in inventory. Up to this point, API integration has focused on specific sets of
data, but what happens when you need to move a large amount of
PAGINATION data from one system to another? Some applications expose this
There might be way too much data, or data that just keeps going ability with Bulk APIs. Bulk APIs allow you to update, insert, and
and scrolling. This is where pagination comes in. Pagination is the delete a large number of records all at once. This can be particularly
ability to put that giant pool of responses from every customer useful when transferring large systems of record (e.g., marketing
you’ve had since 1994 into human-readable pages without seizing automation, CRM, or ERP) from one provider to another.
up your computer. Pagination requires some implied order by a
Bulk actually operates in several batches of data sets when a request
field, or fields, like unique id, created date, modified date, and so on.
for a bulk job is sent to the application provider. The application
There are a few different types of pagination you might encounter.
provider will send batches over asynchronously to complete the
OFFSET job. Depending on the size of the data set, the job will also send you
This is easiest to implement and is therefore very common. a unique identifier to check the job status and close the job once
The LIMIT is the number of rows that will be returned, and complete. Be sure to double-check the file type that a bulk API will
OFFSET is the starting point of results. For example, a path of provide — either CSV, JSON, or XML.
/widgets?limit=10 would return 10 rows for the first page. Then
Additionally, if you’re not getting the full data set back, be sure to
the second page could be /widgets?limit=10&offset=10.
check any API rate limits that apply, as you may exceed them with
This would return 10 rows, starting with the 10 row, and so on. The
th large data transfers.
downside of using offset pagination is it becomes less performant
Figure 4: Bulk data transfer
with large data sets. For example, if the offset is 1M rows, it will still
have to run through 1M rows before returning the limit requested.
KEYSET
A keyset is helpful to get around large data sets and uses the filter
value of the last page of results as the starting point for the next page
of results using an additional limit URL parameter. For example, the
first call could be GET /widgets?limit=10 with either a unique
identifier like date created or modified. The second call would be
GET /widgets?limit=10&created:lte:2019-09, the next would
be GET /widgets?limit=10&created:lte:2019-10, and so on.
ADDITIONAL RESOURCES
*Books from the “Foundations of RESTful Architecture” Refcard:
• Richardson, L., & Amundsen, M. (2015). RESTful web APIs.
O’Reilly. WRITTEN BY BRIAN BUSCH,
• Allamaraju, S. (2010). RESTful web services cookbook. PRODUCT MARKETING & ALLIANCES,
CLOUD ELEMENTS
O’Reilly.
• Masse, M. (2011). REST API design rulebook: Designing Brian Busch is an API enthusiast, leads
consistent RESTful web service interfaces. O’Reilly. Product and Alliances Marketing for Cloud
Elements, and has been involved in launching
• Biehl, M. (2016). RESTful API design. CreateSpace. and scaling new products and services throughout his
• Webber, J., Parastatidis, S., & Robinson, I. (2010). REST in career, most recently with Kapost (now Upland Kapost) and
practice. O’Reilly. before that, Captricity (now Vidado.ai).
• Louvel, J. (2013). Restlet in action: Developing RESTful web He holds degrees from Boston College and an MBA from UC
APIs in Java. Manning. Berkeley-Haas. @brbusch
• Kalali, M., & Mehta, B. (2013). Developing RESTful services
with JAX-RS 2.0, WebSockets, and JSON. Packt Publishing.