The Instructure Community will enter a read-only state on November 22, 2025 as we prepare to migrate to our new Community platform in early December.
Read our blog post for more info about this change.
Found this content helpful? Log in or sign up to leave a like!
Hi,
I am trying to use API to create a Canvas page with page body. I am using the library from https://goo.gl/6j8hzo
This is in Google Apps Script.
var pageData =
{
"url": "api2",
"title": "api2",
"created_at": "datetime",
"updated_at": "datetime",
"hide_from_students": false,
"editing_roles": "",
"last_edited_by": "User",
"body": "<p>content1</p>",
"published": false,
"front_page": false,
"locked_for_user": false,
"lock_info": "LockInfo",
"lock_explanation": ""
}
var createPage = canvasAPI("POST /v1/courses/112233/pages", pageData );
The page could be created but there is no content. So the body value is not there. Does anyone h##ave ideas about why?
Thanks,
Chun
@chun_li ,
It was interesting to see that someone linked to my script. It's a very old version, but that's probably not the issue.
The real problem is that your request isn't in the format that Canvas is expecting so it can't find stuff. The correct format is found in the Create page endpoint of the Pages API.
There you'll see that everything starts with wiki_page, that's missing from your request. In JSON, you need a top-level called wiki_page and then everything else is an object that is the property for it.
Also, you're sending some stuff that doesn't make sense ... like last edited by and lock_info. Actually, most of your request isn't supported when you create a page. The only required item is the title.
Here's what should work for the value of pageData:
{
"wiki_page": {
"title": "api2",
"body": "<p>content1</p>",
"published": false,
"front_page": false
}
}Note that the front_page is optional and defaults to false. You could add editing_roles in there, but I think it defaults to teachers if it's blank. What you had was setting it so that no one could edit it.
Thanks, James Jones for your swift reply! And your shared library is very helpful!
I updated the pageData with your suggestion. Then tried:
var createPage = canvasAPI("POST /v1/courses/112233/pages", pageData );
var createPage = canvasAPI("PUT /v1/courses/112233/pages/api2", pageData );
But got error
Request failed for https://uoacanvas/api/v1/courses/112233/pages returned code 500. Truncated server response: {"errors":[{"message":"An error occurred.","error_code":"internal_server_error"}],"error_report_id":23962006} (use muteHttpExceptions option to examine full response). (line 198, file "CanvasAPI")
Any ideas?
Kind regards,
Chun
@Chun Li,
I'm not familiar with Google Apps scripts, but here's how you can create a page and add a page body with PowerShell.
########Tested with PowerShell 5.1#################################################
########Edit these variables to correct information################################
$token = 'Token' # Canvas API Access token
$domain = "Domain" # Example: college.beta.instructure.com
$course = 'Course ID' # Course id for course you want to create page in
$name = 'Page Name' # Name of page
$page_body = 'Page Body' # Put content here that you want in the body of the page
$published = 'True' # Enter True or False
########Do not edit below##########################################################
$headers = @{"Authorization"="Bearer "+$token}
$pageURL = "https://$domain/api/v1/courses/$course/pages"
$Body = @{"wiki_page[title]"="$name"; "wiki_page[body]"="$page_body"; "wiki_page[published]"="$published"}
$Create_page = Invoke-RestMethod -Headers $headers -Method Post -Uri $pageURL -Body $Body
Thanks all, problem solved by using the basic Google apps script POST. I will try the PowerShell way!
James, thanks for the help with how to write the data (payload) for Canvas API!
There's someone else using my script experiencing the same issue with POST. I didn't have much need to do POST when I wrote it, so it is entirely possible that it doesn't work with POST because of a bug. The script has changed a few times over the years, but I don't know that I've ever touched the POST stuff since I just don't use Google Apps to create much.
@chun_li ,
I know you got this working, but I thought I'd share what I had to do to the canvasAPI.gs script to make it work.
I added the content-type and accept headers to the Authorization header. Technically, the Accept header shouldn't be required, but it will keep Canvas from sending while(1); at the start of the output if it's there.
var parms = {
'headers': {
'Authorization': 'Bearer ' + userProperties.token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
I then modified the code for the POST/PUT portion from just payload to JSON.stringify(payload).
} else if (parms.method == 'post' || parms.method == 'put') {
parms.payload = JSON.stringify(payload);
}I have not done any testing beyond creating a wiki page, so I don't know what effects, if any, it will have on other things.
Thanks for updating the CanvasAPI.gs!
Right now, it's only been updated in the messages here in the Community. The places that people are referring to aren't my copies of the script, they're copies that other people have made and shared. I have a copy on GitHub, but I want to do additional testing before I update it.
Community helpTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in