Authentication
Before you can start making requests towards Sendcloud APIs, register and obtain your Public
and Secret
API keys and learn how to authenticate your requests.
When generating an API integration, take a look at the following settings:
- Service Points: allows the API integration to be used in
servicepoint.sendcloud.sc
API. - Use OAuth2 authentication: enforces the integration to use OAuth2 authentication instead of basic, otherwise it will fail.
Basic Authentication
Sendcloud uses Basic Authentication for authenticating requests for APIs, where username is your Public Key
and password your Private Key
as provided for your integration.
Authenticating your requests
Once you have obtained your Public
and Private
key you will need to start authenticating your requests, to do this, include your keys in your requests.
To authenticate a request to the Sendcloud API, use curl
:
1curl --location 'https://panel.sendcloud.sc/api/v2/parcels' \
2--header 'Accept: application/json' \
3--header 'Authorization: Basic <based-64-encoded-token>'
In the example above, the Authorization
header value is constructed by combining the API keys (base64-encoded) separated by a colon (:
).
You can also use the Python requests library. In this case, the base64 encoding is handled automatically, you only have to pass you public and private key.
1import requests
2
3# Replace 'your_sendcloud_public_key' and 'your_sendcloud_private_key' with your actual API keys
4public_key = "your_sendcloud_public_key"
5private_key = "your_sendcloud_private_key"
6
7r = requests.get('https://panel.sendcloud.sc/api/v2/parcels', auth=(public_key, private_key))
OAuth2 Authentication
OAuth2 provides a token-based authentication mechanism. Obtain an access token by authenticating with the OAuth2 server, then include the obtained token in the Authorization
headers of your API requests.
To generate an API integration for OAuth2, make sure to check the Use OAuth2 authentication checkbox when creating it.
1# generate an OAuth2 access token
2curl -X POST --location "https://account.sendcloud.com/oauth2/token" \
3 --header "Content-Type: application/x-www-form-urlencoded" \
4 -d "grant_type=client_credentials&scope=api" \
5 --basic --user your_sendcloud_public_key:your_sendcloud_private_key
The response of previous request will be similar to the following JSON:
1{
2 "access_token": "ory_at_dK...",
3 "expires_in": 3599,
4 "scope": "api",
5 "token_type": "bearer"
6}
This request returns an access token, which you can use in subsequent API requests:
1# make a request to Sendcloud API using the OAuth2 authentication
2curl --location "https://panel.sendcloud.sc/api/v2/user" --header "Authorization: Bearer ${access_token}"
The following is a Python example using requests
library:
1import requests
2
3# OAuth2 token endpoint
4token_url = "https://account.sendcloud.com/oauth2/token"
5
6# API endpoint
7api_url = "https://panel.sendcloud.sc/api/v2/user"
8
9# OAuth2 client credentials
10client_id = "your_client_id"
11client_secret = "your_client_secret"
12
13# Requesting OAuth2 token
14token_data = {
15 "grant_type": "client_credentials",
16 "scope": "api",
17}
18
19# Obtain OAuth2 token
20token_response = requests.post(token_url, data=token_data, auth=(client_id, client_secret))
21access_token = token_response.json().get("access_token")
22
23# Making API request with OAuth2 authentication
24api_headers = {"Authorization": f"Bearer {access_token}"}
25api_response = requests.get(api_url, headers=api_headers)
26
27# Display API response
28print("API Request Successful:")
29print(api_response.json())