Many service providers create libraries to make it easier to use their services. These libraries are often called SDKs or client libraries.
“Client” in this context refer to you (the developer) because you’re a client to their business. It doesn’t refer to the browser in the Client-Server context.
How client libraries work
Most client libraries are made for backend languages. For example, Stripe has client libraries for Ruby, Python, and Node, amongst others.
You can download these client libraries with the language’s ecosystem. For example, you can install Stripe with npm.
npm install stripe -SYou can then require the client library in your servers.
const stripe = require('stripe')Client libraries usually help you with two things:
- Authentication and Authorization
- Call specific endpoints in the API in a readable way
Authentication and Authorization
We don’t need the OAuth authentication flow here. Most of the time, you’ll use one or more of the following credentials:
- An access token
- An API Key
- An API Secret
- A Service Key
For example, you can authenticate Stripe with an API Key.
// Authentication in Stripeconst Stripe = require('stripe')const stripe = Stripe('YOUR_API_KEY')Once you do this, you can use the client library (stripe in this case) without further having to send in an Authorization header for all your requests.
// Use stripe normallystripe.charges.create(/*...*/)Calling APIs without using endpoints
Client libraries let you call API without remembering the endpoints.
For example, let’s say you want to charge a card with Stripe. If you use the client library, you can use the following code:
const charge = await stripe.charges.create({ amount: 2000, currency: 'usd',})If you use REST API, you have to use the following code:
// Does the same thing as aboveconst response = await zlFetch('https://api.stripe.com/v1/charges', { method: 'post', auth: 'your-api-key', body: { amount: 2000, currency: 'usd', },})Code from the client library is easier to read because:
stripe.charges.createis self-explanatory — you’re creating a stripe charge.- The code is shorter and more concise
Should you use client libraries?
Yes — because most client libraries are easier to use compared to raw requests.
You may not want to use client libraries in the following cases:
- When the REST API documentation is better than the client library’s documentation
- When you only need 1-2 API requests and you can do it faster with raw REST APIs
3rd party libraries vs client libraries
Many developers create their own version of client libraries. They do this when the API doesn’t have a client library.
3rd-party libraries can go unmaintained after a while. Be aware of this risk if you use them. Use your best judgment and discretion. Personally, I don’t recommend using 3rd-party libraries unless they’re popular and highly used.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson