Software development can be a complex and daunting field, especially for those who are new to it. The tech world's jargon and acronyms can be confusing to newcomers. You may have heard the term “SDK.” But what exactly is an SDK, and why is it important for software development?
More specifically, how can an SDK, when applied to an API, create huge benefits for your API management?
In this article, we'll take a closer look at what an SDK is and why it's an essential tool for developers looking to create high-quality software applications. You'll also come away with a clear understanding of the benefits SDKs have on API management, for both API owners and end users.
What is an SDK?
An SDK (software development kit) is a programming library made to serve developers and the development process.
A good use case for SDKs are APIs, the application programming interfaces that allow two programs, applications or services to communicate with each other. Without an SDK, a developer has to access the API via manual operations. Whereas with an SDK, developers can interact with the API using pre-built functionality, enabling quicker and safer software development.
How to Use SDKs for APIs
An API is the interface of an application by which a developer can interact directly with that application. An SDK provides tools that help a developer interact with the API.
To emphasize how using an SDK is different from interacting with an API via “manual operations,” we will juxtapose calling The One API to find a book with both methods below.
Calling The One API via Manual Operations
We will call The One API via a BASH script and JavaScript fetch method — both “manually.”
curl -X GET https://the-one-api.dev/v2/book/1264392547 \
2 -H "Accept: application/json" \
3 -H "Authorization: Bearer THEONEAPISDK_BEARER_TOKEN"
This is a very basic way to query a server. It is available straight out of the terminal and gives you a way to describe the network request using arguments to one big command.
Explanation about the command:
curl
****the command for BASH to transfer data.-X
debugging mode, allows BASH to be more verbose.GET
is the method.-H
is a header option.
In other words: transfer data , do it with a get
, and pass the headers Accept: application/json
, Authorization: Bearer
…
JavaScript Fetch Method
async function fetchData() {
try {
const url = "https://the-one-api.dev/v2/book/123";
const res = await fetch(url, {
headers: {
Authorization: `Bearer 12345`,
},
});
if (!res.ok) {
throw new Error(res.statusText);
}
const data = await res.json();
console.log(data);
} catch (error) {
console.log("error", error);
}
}
This is the most basic way to query a server with JavaScript. What you see here is an asynchronous function that queries the server, converts the request to JSON format and then logs the data it produced.
It is better than bash because:
- Readability: Instead of arguments to one long command, you'd use an object which is more human-readable and less error-prone.
- It handles an error. Instead of just logging the error, the
try
/catch
block allows you to handle the situation where an error occurred.
Calling The One API via an SDK
import { TheOneApiSDK } from "./src";
const sdk = new TheOneApiSDK(process.env.THEONEAPISDK_BEARER_TOKEN);
(async () => {
const result = await sdk.Book.getBook("123");
console.log(result);
})();
Notice that the SDK client allows us to use the book controller and getBook method to query the API. We set the headers when we instantiated the clients and we were ready to query the API. This approach is much easier to read and less error-prone.
This example is different from the two mentioned above because the user did not write the http request.
The http request is actually made behind the scenes (might be written with JS fetch too) by the maintainer of the SDK; this allows the maintainer to decide:
- What network protocol is going to be used.
- What is the destination of the network request.
- How the headers should be set for the network request.
- Readability: It's very clear which action the user wants to achieve, what is the controller, etc.
Who Benefits from SDKs?
There are many benefits to using an SDK. For the end users, it allows safer and cleaner access to the API. For the owners it ensures the API is used correctly and keeps support costs down.
SDK can reduce costs in many ways, including:
Retry strategy. When writing an SDK, you can add logic that prevents an SDK client from keep trying to query the API, therefore preventing unwanted calls to the API
Better use. Because the user does not query the API directly, the API receives better calls or more standardized input from its clients.
Speed up development. The SDK clients enjoy faster development because the requests from the server are standardized.
Code maintenance. When querying the API directly, you need to keep up with every update the API has done. Using an SDK facilitates this interaction and keeps up with the updates of the API. You do, however, need to keep your SDK up to date.
An SDK benefits the API user by:
- Better understanding how to use an API through semantically-named functions, parameters and types.
- Making it easier to invoke methods as they become readily available/discoverable via internal development environments (IDE)
- Provides easier API access, with simple functions and parameters.
- Prevents bad requests, and allows the user to correct their input.
An SDK benefits the API owner by:
- Ensuring the required inputs from the user are in every request.
- Preventing wrong inputs from being sent to the API server via enforcing types and validations.
- Having an additional layer of validations to enforce response patterns. For example, if a user is sending too many requests, the SDK can warn and stop the user as they approach their limit.
Conclusion
Many API owners don't provide SDKs because of the difficulty and the development time involved with creating one, not to mention the onerous task of maintaining them. An API can have dozens or even hundreds of endpoints, and each one requires a function definition.