Auto Sort sheet using API - Python
I have a code that should work to auto-sort my sheet, but I keep getting the message below. Others have used this script with no problem. Any advice would be greatly appreciated.
Error sorting sheet. Status code: 405
"errorCode" : 1122,
"message" : "Requested URL does not support this method: PUT",
Here is the script I used:
import requests
import json
# Smartsheet API access token
access_token = {'YOUR API TOKEN HERE'}
# Smartsheet sheet ID
sheet_id = {'YOUR SHEET ID HERE'}
# Define the endpoint for the API
url = f'https://api.smartsheet.com/2.0/sheets/{sheet_id}/sort'
# Define the headers
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Define the sorting criteria
sort_criteria = [
{
"columnId": {YOUR COLUMN ID HERE},
"direction": "ASC" # or "DESC" for descending
}
]
# Define the payload
payload = {
"sortSpecifiers": sort_criteria
}
# Send the request to Smartsheet API
response = requests.put(url, headers=headers, data=json.dumps(payload))
# Check if the request was successful
if response.status_code == 200:
print("Sheet sorted successfully.")
else:
print(f"Error sorting sheet. Status code: {response.status_code}")
print(response.text)
Answers
-
You need to use HTTP POST and not PUT. Change the line:
# Send the request to Smartsheet API response = requests.put(url, headers=headers, data=json.dumps(payload))
to
response = requests.post(url, headers=headers, data=json.dumps(payload))
Additionally, while there are times to directly program the API, I highly recommend using Smartsheet's SDK for your language if available. This will take care of many issue, such as retrying the API calls if your run into time outs due to too many calls. The Python SDK is here:
Good Luck
Categories
- All Categories
- 14 Welcome to the Community
- Smartsheet Customer Resources
- 64.1K Get Help
- 414 Global Discussions
- 221 Industry Talk
- 461 Announcements
- 4.8K Ideas & Feature Requests
- 143 Brandfolder
- 141 Just for fun
- 58 Community Job Board
- 462 Show & Tell
- 32 Member Spotlight
- 1 SmartStories
- 299 Events
- 38 Webinars
- 7.3K Forum Archives