API wrapper for the Canvas LMS
npx @tessl/cli install tessl/pypi-canvasapi@3.3.0A comprehensive Python library that provides programmatic access to Instructure's Canvas Learning Management System API. CanvasAPI enables developers to manage courses, users, gradebooks, assignments, and other Canvas resources with a chainable interface, automatic pagination handling, and robust error management.
pip install canvasapifrom canvasapi import CanvasImport specific classes as needed:
from canvasapi import Canvas
from canvasapi.course import Course
from canvasapi.user import User
from canvasapi.assignment import Assignment
from canvasapi.discussion_topic import DiscussionTopic
from canvasapi.file import File
from canvasapi.folder import Folder
from canvasapi.group import Group
from canvasapi.account import Account
from canvasapi.enrollment import Enrollment
from canvasapi.quiz import Quiz
from canvasapi.page import Page
from canvasapi.module import Module
from canvasapi.section import Section
from canvasapi.submission import Submission
from canvasapi.conversation import Conversation
from canvasapi.calendar_event import CalendarEvent
from canvasapi.paginated_list import PaginatedListDirect class imports from main package:
from canvasapi.exceptions import (
CanvasException,
BadRequest,
Unauthorized,
Forbidden,
ResourceDoesNotExist,
RequiredFieldMissing
)from canvasapi import Canvas
# Initialize Canvas API connection
canvas = Canvas("https://your-canvas-url.com", "your-access-token")
# Get current user
user = canvas.get_current_user()
print(f"Hello, {user.name}!")
# Get user's courses
courses = canvas.get_courses()
for course in courses:
print(f"Course: {course.name}")
# Get a specific course and its assignments
course = canvas.get_course(12345)
assignments = course.get_assignments()
for assignment in assignments:
print(f"Assignment: {assignment.name} (Due: {assignment.due_at})")
# Create a new assignment
new_assignment = course.create_assignment({
'name': 'New Assignment',
'description': 'This is a new assignment',
'due_at': '2024-12-31T23:59:59Z',
'points_possible': 100
})CanvasAPI uses an object-oriented design that mirrors Canvas's API structure:
The library converts JSON API responses into Python objects with a chainable interface, enabling intuitive navigation between related Canvas entities (e.g., course.get_assignments() or user.get_enrollments()).
The primary Canvas class that provides access to all Canvas resources including accounts, courses, users, and global operations.
class Canvas:
def __init__(self, base_url: str, access_token: str): ...
def get_current_user(self) -> CurrentUser: ...
def get_course(self, course, use_sis_id: bool = False) -> Course: ...
def get_courses(self, **kwargs) -> PaginatedList[Course]: ...
def get_user(self, user, id_type: str = None) -> User: ...
def get_account(self, account, use_sis_id: bool = False) -> Account: ...
def get_accounts(self, **kwargs) -> PaginatedList[Account]: ...Comprehensive course operations including content creation, enrollment management, assignments, quizzes, and gradebook functionality.
class Course(CanvasObject):
def get_assignments(self, **kwargs) -> PaginatedList[Assignment]: ...
def create_assignment(self, assignment: dict, **kwargs) -> Assignment: ...
def get_users(self, **kwargs) -> PaginatedList[User]: ...
def enroll_user(self, user, enrollment_type: str, **kwargs) -> Enrollment: ...
def get_modules(self, **kwargs) -> PaginatedList[Module]: ...
def create_module(self, module: dict, **kwargs) -> Module: ...User profile operations, enrollment handling, file management, and communication channel administration.
class User(CanvasObject):
def get_profile(self, **kwargs) -> dict: ...
def edit(self, **kwargs) -> User: ...
def get_courses(self, **kwargs) -> PaginatedList[Course]: ...
def get_enrollments(self, **kwargs) -> PaginatedList[Enrollment]: ...
def get_files(self, **kwargs) -> PaginatedList[File]: ...
def create_folder(self, name: str, **kwargs) -> Folder: ...Assignment creation, submission handling, grading workflows, rubrics, and grade management.
class Assignment(CanvasObject):
def get_submissions(self, **kwargs) -> PaginatedList[Submission]: ...
def get_submission(self, user, **kwargs) -> Submission: ...
def create_override(self, assignment_override: dict, **kwargs) -> AssignmentOverride: ...
def edit(self, **kwargs) -> Assignment: ...
def delete(self, **kwargs) -> Assignment: ...Conversations, announcements, messaging, and notification management within Canvas.
def get_conversations(self, **kwargs) -> PaginatedList[Conversation]: ...
def create_conversation(self, recipients: list, body: str, **kwargs) -> list[Conversation]: ...
def get_announcements(self, context_codes: list, **kwargs) -> PaginatedList[DiscussionTopic]: ...
def conversations_mark_all_as_read(self, **kwargs) -> bool: ...Pages, files, folders, modules, external tools, and content organization within courses.
class Course(CanvasObject):
def get_pages(self, **kwargs) -> PaginatedList[Page]: ...
def create_page(self, page: dict, **kwargs) -> Page: ...
def get_files(self, **kwargs) -> PaginatedList[File]: ...
def get_folders(self, **kwargs) -> PaginatedList[Folder]: ...
def create_folder(self, name: str, **kwargs) -> Folder: ...Quiz creation, question management, submissions, and both Classic Quizzes and New Quizzes support.
class Course(CanvasObject):
def get_quizzes(self, **kwargs) -> PaginatedList[Quiz]: ...
def create_quiz(self, quiz: dict, **kwargs) -> Quiz: ...
def create_new_quiz(self, **kwargs) -> NewQuiz: ...
class Quiz(CanvasObject):
def get_questions(self, **kwargs) -> PaginatedList[QuizQuestion]: ...
def create_question(self, question: dict, **kwargs) -> QuizQuestion: ...Account management, user creation, role administration, SIS imports, and institutional-level operations.
class Account(CanvasObject):
def create_user(self, user: dict, **kwargs) -> User: ...
def get_users(self, **kwargs) -> PaginatedList[User]: ...
def create_course(self, **kwargs) -> Course: ...
def create_subaccount(self, account: dict, **kwargs) -> Account: ...
def create_admin(self, user_id: int, **kwargs) -> Admin: ...class CanvasException(Exception):
"""Base exception for all Canvas API errors"""
class BadRequest(CanvasException):
"""HTTP 400 Bad Request errors"""
class Unauthorized(CanvasException):
"""HTTP 401 Unauthorized errors"""
class Forbidden(CanvasException):
"""HTTP 403 Forbidden errors"""
class ResourceDoesNotExist(CanvasException):
"""HTTP 404 Not Found errors"""
class RequiredFieldMissing(CanvasException):
"""Missing required parameters"""class PaginatedList:
"""Handles Canvas API pagination automatically"""
def __iter__(self): ...
def __getitem__(self, index): ...
class CanvasObject:
"""Base class for all Canvas API objects"""
def set_attributes(self, attributes: dict): ...
class CurrentUser(User):
"""Extended user class for the currently authenticated user"""
class Section(CanvasObject):
"""Course section representation"""
class Outcome(CanvasObject):
"""Learning outcome representation"""
class OutcomeGroup(CanvasObject):
"""Learning outcome group representation"""
class PlannerNote(CanvasObject):
"""Planner note representation"""
class PlannerOverride(CanvasObject):
"""Planner override representation"""
class Poll(CanvasObject):
"""Poll representation"""
class EPortfolio(CanvasObject):
"""Electronic portfolio representation"""
class CourseEpubExport(CanvasObject):
"""Course EPUB export representation"""
class CommMessage(CanvasObject):
"""Communication message representation"""
class AccountCalendar(CanvasObject):
"""Account calendar representation"""
class AppointmentGroup(CanvasObject):
"""Appointment group representation"""
class CalendarEvent(CanvasObject):
"""Calendar event representation"""
class JWT(CanvasObject):
"""JSON Web Token representation"""
class Progress(CanvasObject):
"""Progress tracking for asynchronous operations"""
class Todo(CanvasObject):
"""Todo item representation"""
class OutcomeLink(CanvasObject):
"""Outcome link representation"""
class OutcomeResult(CanvasObject):
"""Outcome result representation"""
class Rubric(CanvasObject):
"""Rubric representation"""
class RubricAssociation(CanvasObject):
"""Rubric association representation"""
class BlueprintTemplate(CanvasObject):
"""Blueprint template representation"""
class BlueprintSubscription(CanvasObject):
"""Blueprint subscription representation"""
class LatePolicy(CanvasObject):
"""Late policy representation"""
class GradingStandard(CanvasObject):
"""Grading standard representation"""
class CourseStudentSummary(CanvasObject):
"""Course student summary analytics representation"""
class ExternalTool(CanvasObject):
"""External tool representation"""