This was fun and I learned something new.
There's no endpoint for the calendar to turn them on or off. So, I opened the network tab in Chrome dev tools to see what happens when you click on the calendar button. It shoots a POST request off to an endpoint:
https://domain.instructure.com/api/v1/calendar_events/save_course_contexts
There's a form attached to the request that has selected_contexts[] field for each course you want activated on the calendar, formatted as course_12345. In the UI, each time you click the checkbox, the form is submitted with each course in an array. Removing a course from the UI removes it from the form submission body.
This works the same for users. Instead of submitting course_12345, submit user_12345 and that will enable (if included) or disable (if not included) their personal calendar on the next page reload.
So, as long as you know the course or user ID, you can set/unset any calendar you want via the API.
curl
curl --location --request POST 'https://elkhart.instructure.com/api/v1/calendar_events/save_selected_contexts' \
--header 'Authorization: Bearer yourApiKey12345' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'selected_contexts[]=course_12345' \
--data-urlencode 'selected_contexts[]=course_98765'
python
import requests
url = "https://yourDomain.instructure.com/api/v1/calendar_events/save_selected_contexts"
payload='selected_contexts[]=course_38237'
headers = {
'Authorization': 'Bearer yourApiKey',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text) # { status: ok }