Skip to main content

What is an SDK for a REST API?

ยท 8 min read
Dylan Huang

SDKs makes the integration of REST APIs easier by packaging boilerplate code for developers.

Why do developers need help integrating APIs?โ€‹

Let's take a look at the numbers.

Plaid has a REST API with 215 operations, 1346 schemas, and 215 parameters.

SnapTrade has a REST API with 91 operations, 125 schemas, and 183 parameters.

Twilio has a REST API with 199 operations, 152 schemas, and 874 parameters.

The complexity of these APIs can be daunting for developers who are under a time pressure to integrate an API and frustrating for API companies because onboarding is time consuming. Developers are required to parse tons of documentation and code while API companies must provide continuous support for a successful partnership. To make matters worse, developers can be using a programming language that nobody at the API company knows. To alleviate the pain, best-in-class API companies publish SDKs to accelerate the integration process.

So how does an SDK actually help?โ€‹

Lets walkthrough an example in Python integrating Twilio's API without an SDK. Then we will rewrite our implementation using their SDK. Finally we will analyze the significant difference between implementations.

For this example we send an SMS and retrieve the sent SMS by ID.

Without an SDKโ€‹

Import your favorite request library to make HTTP requests. In this case we are using Python's requests library. Then lets initialize all our parameters.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Construct the target URL for our HTTP request. We found the URL from Twilio's documentation.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Construct our request body using the format from the documentation.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Construct our authentication credentials according to how Twilio receives credentials and how the requests library performs basic authentication which in this case is a tuple where the first item is the username and the second item is the password.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Make a POST request using the requests library and cross our fingers ๐Ÿคž that we got all the previous steps right with no typos.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Deserialize the response from JSON and retrieve the sid field as shown in an example response from the documentation.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

To retrieve the message we construct a URL based on the format described in the documentation.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Make a GET request with the new URL while remembering to pass in the auth variable we assigned earlier.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Import your favorite request library to make HTTP requests. In this case we are using Python's requests library. Then lets initialize all our parameters.

Construct the target URL for our HTTP request. We found the URL from Twilio's documentation.

Construct our request body using the format from the documentation.

Construct our authentication credentials according to how Twilio receives credentials and how the requests library performs basic authentication which in this case is a tuple where the first item is the username and the second item is the password.

Make a POST request using the requests library and cross our fingers ๐Ÿคž that we got all the previous steps right with no typos.

Deserialize the response from JSON and retrieve the sid field as shown in an example response from the documentation.

To retrieve the message we construct a URL based on the format described in the documentation.

Make a GET request with the new URL while remembering to pass in the auth variable we assigned earlier.

without-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Whew ๐Ÿ˜ช we did it. It only took 7 trips back and forth from the documentation but we finally did it. Now lets take a look at what its like to implement the same flow with Twilio's SDK.

With an SDK
โ€‹

Take a final look at what it looks like to implement this flow without an SDK because you'll never see it again.

with-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Replace the requests import with Twilio's Python SDK

with-sdk.py

_33
from twilio.rest import Client
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

Remove the URL and request body variables as the SDK handles this for us.

with-sdk.py

_24
from twilio.rest import Client
_24
_24
# Twilio account credentials
_24
account_sid = 'YOUR_ACCOUNT_SID'
_24
auth_token = 'YOUR_AUTH_TOKEN'
_24
_24
# Set the sender's and recipient's phone numbers
_24
from_number = '+1234567890' # Your Twilio phone number
_24
to_number = '+9876543210' # Destination phone number
_24
_24
# Set the message content
_24
message = 'Hello from Twilio!'
_24
_24
# Step 1: Send SMS
_24
auth = (account_sid, auth_token)
_24
send_response = requests.post(url, data=send_data, auth=auth)
_24
_24
sms_sid = send_response.json()['sid']
_24
_24
# Step 2: Retrieve SMS details
_24
retrieve_url = f'{url}/{sms_sid}.json'
_24
retrieve_response = requests.get(retrieve_url, auth=auth)
_24
_24
sms_data = retrieve_response.json()

Insantiate Twilio's client with credentials instead of creating a raw tuple for our credentials. If we made a typo or forgot to pass our credentials the SDK will let us know ahead of time.

with-sdk.py

_24
from twilio.rest import Client
_24
_24
# Twilio account credentials
_24
account_sid = 'YOUR_ACCOUNT_SID'
_24
auth_token = 'YOUR_AUTH_TOKEN'
_24
_24
# Set the sender's and recipient's phone numbers
_24
from_number = '+1234567890' # Your Twilio phone number
_24
to_number = '+9876543210' # Destination phone number
_24
_24
# Set the message content
_24
message = 'Hello from Twilio!'
_24
_24
# Step 1: Send SMS
_24
client = Client(account_sid, auth_token)
_24
send_response = requests.post(url, data=send_data, auth=auth)
_24
_24
sms_sid = send_response.json()['sid']
_24
_24
# Step 2: Retrieve SMS details
_24
retrieve_url = f'{url}/{sms_sid}.json'
_24
retrieve_response = requests.get(retrieve_url, auth=auth)
_24
_24
sms_data = retrieve_response.json()

Call client.messages.create and pass in the from_, to, and body parameters. If we make a typo here the SDK will let us know ahead of time.

with-sdk.py

_28
from twilio.rest import Client
_28
_28
# Twilio account credentials
_28
account_sid = 'YOUR_ACCOUNT_SID'
_28
auth_token = 'YOUR_AUTH_TOKEN'
_28
_28
# Set the sender's and recipient's phone numbers
_28
from_number = '+1234567890' # Your Twilio phone number
_28
to_number = '+9876543210' # Destination phone number
_28
_28
# Set the message content
_28
message = 'Hello from Twilio!'
_28
_28
# Step 1: Send SMS
_28
client = Client(account_sid, auth_token)
_28
send_response = client.messages.create(
_28
from_=from_number,
_28
to=to_number,
_28
body=message
_28
)
_28
_28
sms_sid = send_response.json()['sid']
_28
_28
# Step 2: Retrieve SMS details
_28
retrieve_url = f'{url}/{sms_sid}.json'
_28
retrieve_response = requests.get(retrieve_url, auth=auth)
_28
_28
sms_data = retrieve_response.json()

Retrieve the sid value from the response without calling .json().

with-sdk.py

_28
from twilio.rest import Client
_28
_28
# Twilio account credentials
_28
account_sid = 'YOUR_ACCOUNT_SID'
_28
auth_token = 'YOUR_AUTH_TOKEN'
_28
_28
# Set the sender's and recipient's phone numbers
_28
from_number = '+1234567890' # Your Twilio phone number
_28
to_number = '+9876543210' # Destination phone number
_28
_28
# Set the message content
_28
message = 'Hello from Twilio!'
_28
_28
# Step 1: Send SMS
_28
client = Client(account_sid, auth_token)
_28
send_response = client.messages.create(
_28
from_=from_number,
_28
to=to_number,
_28
body=message
_28
)
_28
_28
sms_sid = send_response.sid
_28
_28
# Step 2: Retrieve SMS details
_28
retrieve_url = f'{url}/{sms_sid}.json'
_28
retrieve_response = requests.get(retrieve_url, auth=auth)
_28
_28
sms_data = retrieve_response.json()

Replace our second request with a simple method call.

with-sdk.py

_25
from twilio.rest import Client
_25
_25
# Twilio account credentials
_25
account_sid = 'YOUR_ACCOUNT_SID'
_25
auth_token = 'YOUR_AUTH_TOKEN'
_25
_25
# Set the sender's and recipient's phone numbers
_25
from_number = '+1234567890' # Your Twilio phone number
_25
to_number = '+9876543210' # Destination phone number
_25
_25
# Set the message content
_25
message = 'Hello from Twilio!'
_25
_25
# Step 1: Send SMS
_25
client = Client(account_sid, auth_token)
_25
send_response = client.messages.create(
_25
from_=from_number,
_25
to=to_number,
_25
body=message
_25
)
_25
_25
sms_sid = send_response.sid
_25
_25
# Step 2: Retrieve SMS details
_25
sms = client.messages(sms_sid).fetch()

By using the SDK we reduced the lines of code by 24% and removed 31% of the variables ๐ŸŽ‰.

with-sdk.py

_25
from twilio.rest import Client
_25
_25
# Twilio account credentials
_25
account_sid = 'YOUR_ACCOUNT_SID'
_25
auth_token = 'YOUR_AUTH_TOKEN'
_25
_25
# Set the sender's and recipient's phone numbers
_25
from_number = '+1234567890' # Your Twilio phone number
_25
to_number = '+9876543210' # Destination phone number
_25
_25
# Set the message content
_25
message = 'Hello from Twilio!'
_25
_25
# Step 1: Send SMS
_25
client = Client(account_sid, auth_token)
_25
send_response = client.messages.create(
_25
from_=from_number,
_25
to=to_number,
_25
body=message
_25
)
_25
_25
sms_sid = send_response.sid
_25
_25
# Step 2: Retrieve SMS details
_25
sms = client.messages(sms_sid).fetch()

Take a final look at what it looks like to implement this flow without an SDK because you'll never see it again.

Replace the requests import with Twilio's Python SDK

Remove the URL and request body variables as the SDK handles this for us.

Insantiate Twilio's client with credentials instead of creating a raw tuple for our credentials. If we made a typo or forgot to pass our credentials the SDK will let us know ahead of time.

Call client.messages.create and pass in the from_, to, and body parameters. If we make a typo here the SDK will let us know ahead of time.

Retrieve the sid value from the response without calling .json().

Replace our second request with a simple method call.

By using the SDK we reduced the lines of code by 24% and removed 31% of the variables ๐ŸŽ‰.

with-sdk.py

_33
import requests
_33
_33
# Twilio account credentials
_33
account_sid = 'YOUR_ACCOUNT_SID'
_33
auth_token = 'YOUR_AUTH_TOKEN'
_33
_33
# Set the sender's and recipient's phone numbers
_33
from_number = '+1234567890' # Your Twilio phone number
_33
to_number = '+9876543210' # Destination phone number
_33
_33
# Set the message content
_33
message = 'Hello from Twilio!'
_33
_33
# Twilio API endpoint
_33
url = f'https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json'
_33
_33
# Step 1: Send SMS
_33
send_data = {
_33
'From': from_number,
_33
'To': to_number,
_33
'Body': message
_33
}
_33
_33
auth = (account_sid, auth_token)
_33
send_response = requests.post(url, data=send_data, auth=auth)
_33
_33
sms_sid = send_response.json()['sid']
_33
_33
# Step 2: Retrieve SMS details
_33
retrieve_url = f'{url}/{sms_sid}.json'
_33
retrieve_response = requests.get(retrieve_url, auth=auth)
_33
_33
sms_data = retrieve_response.json()

SDK vs. no SDKโ€‹

Lets breakdown through the significant differences that made the SDK code better.

Setupโ€‹

We do not need to manually construct the URLs to make an HTTP request.

with-sdk.py
without-sdk.py

_25
from twilio.rest import Client
_25
_25
# Twilio account credentials
_25
account_sid = 'YOUR_ACCOUNT_SID'
_25
auth_token = 'YOUR_AUTH_TOKEN'
_25
_25
# Set the sender's and recipient's phone numbers
_25
from_number = '+1234567890' # Your Twilio phone number
_25
to_number = '+9876543210' # Destination phone number
_25
_25
# Set the message content
_25
message = 'Hello from Twilio!'
_25
_25
# Step 1: Send SMS
_25
client = Client(account_sid, auth_token)
_25
send_response = client.messages.create(
_25
from_=from_number,
_25
to=to_number,
_25
body=message
_25
)
_25
_25
sms_sid = send_response.sid
_25
_25
# Step 2: Retrieve SMS details
_25
sms = client.messages(sms_sid).fetch()

Authenticationโ€‹

Credentials were stored once and stored as part of the SDK. Notice that we did not need to pass in an auth variable again.

with-sdk.py
without-sdk.py

_25
from twilio.rest import Client
_25
_25
# Twilio account credentials
_25
account_sid = 'YOUR_ACCOUNT_SID'
_25
auth_token = 'YOUR_AUTH_TOKEN'
_25
_25
# Set the sender's and recipient's phone numbers
_25
from_number = '+1234567890' # Your Twilio phone number
_25
to_number = '+9876543210' # Destination phone number
_25
_25
# Set the message content
_25
message = 'Hello from Twilio!'
_25
_25
# Step 1: Send SMS
_25
client = Client(account_sid, auth_token)
_25
send_response = client.messages.create(
_25
from_=from_number,
_25
to=to_number,
_25
body=message
_25
)
_25
_25
sms_sid = send_response.sid
_25
_25
# Step 2: Retrieve SMS details
_25
sms = client.messages(sms_sid).fetch()

Complexityโ€‹

Making HTTP requests were converted to ergonomic method calls so we deleted a variable, removed any mention of POST and GET requests, and removed explicit JSON deserialization. The SDK also would have easily caught incorrect argument names when calling create() and fetch().

with-sdk.py
without-sdk.py

_25
from twilio.rest import Client
_25
_25
# Twilio account credentials
_25
account_sid = 'YOUR_ACCOUNT_SID'
_25
auth_token = 'YOUR_AUTH_TOKEN'
_25
_25
# Set the sender's and recipient's phone numbers
_25
from_number = '+1234567890' # Your Twilio phone number
_25
to_number = '+9876543210' # Destination phone number
_25
_25
# Set the message content
_25
message = 'Hello from Twilio!'
_25
_25
# Step 1: Send SMS
_25
client = Client(account_sid, auth_token)
_25
send_response = client.messages.create(
_25
from_=from_number,
_25
to=to_number,
_25
body=message
_25
)
_25
_25
sms_sid = send_response.sid
_25
_25
# Step 2: Retrieve SMS details
_25
sms = client.messages(sms_sid).fetch()

Conclusionโ€‹

SDKs are effective in reducing the pain a developer feels when integrating a REST API. SDKs throw out manual steps from setup/authentication, reduce the cognitive load for the end developer, and reduce the lines of code. Furthermore, SDKs catch compilation or runtime errors earlier in the development process.

Dylan Huang
Dylan HuangGitHubLinkedIn

Dylan is a Co-Founder at Konfig. Previously, he built SDK & API tooling at C3.ai. He also once built a viral website with over 210,000 unique users.