Sirv REST API – Python examples

On this page

Use the Sirv REST API to perform over 40 common tasks on your Sirv account.

Visit the REST API reference for all methods.

Connect to Sirv with Python

Create an API client via your Sirv account, if you've not created one already. You'll be given a client ID and client Secret.

Now use your client ID and client Secret to request a token:

import requests
import json

url = 'https://api.sirv.com/v2/token'

payload = {
  'clientId': 'ENTER_YOUR_CLIENT_ID',
  'clientSecret': 'ENTER_YOUR_CLIENT_SECRET'
}
headers = {'content-type': 'application/json'}

response = requests.request('POST', url, data=json.dumps(payload), headers=headers)

token = response.json()['token']

Now that you have a token, you can perform any of the 40+ API methods.

Python upload example

Upload a file to your Sirv account.

Example upload script:

url = 'https://api.sirv.com/v2/files/upload'

querystring = {'filename':'/path/to/uploaded-image.jpg'}

payload = open('/path/to/local-image.jpg', 'rb')

headers = {
 'content-type': 'image/jpeg',
 'authorization': 'Bearer %s' % token
}

response = requests.request('POST', url, data=payload, headers=headers, params=querystring)

print(response)

Including the connection to Sirv, to fetch a token, the script would look like this:

import requests
import json

# First, get a token

url = 'https://api.sirv.com/v2/token'

payload = {
  'clientId': 'ENTER_YOUR_CLIENT_ID',
  'clientSecret': 'ENTER_YOUR_CLIENT_SECRET'
}

headers = {'content-type': 'application/json'}

response = requests.request('POST', url, data=json.dumps(payload), headers=headers)

token = response.json()['token']

# Now upload a file

url = 'https://api.sirv.com/v2/files/upload'

querystring = {'filename':'/path/to/uploaded-image.jpg'}

payload = open('/path/to/local-image.jpg', 'rb')

headers = {
  'content-type': 'image/jpeg',
  'authorization': 'Bearer %s' % token
}

response = requests.request('POST', url, data=payload, headers=headers, params=querystring)

print(response)

Support

If you need help connecting with Python or using any of the Sirv REST API methods, please contact the Sirv support team.

Was this article helpful?

Get help from a Sirv expert