Skip to content

Gradescope API ​

Documentation for the Gradescope submission module.

Overview ​

The gradescope.js module handles all communication with the Gradescope API.

Main Function ​

submitAssg() ​

Submit code to a Gradescope assignment.

javascript
import { submitAssg } from './gradescope.js';

const result = await submitAssg(
  code,       // string - The code to submit
  course_id,  // string - Gradescope course ID
  stud_id,    // string - Student ID (optional)
  asg_id,     // string - Assignment ID
  file_name,  // string - Filename for submission
  token,      // string - Gradescope authenticity token
  cookie      // string - Gradescope session cookie
);

Parameters ​

ParameterTypeRequiredDescription
codestringYesPython code to submit
course_idstringYesGradescope course ID
stud_idstringNoStudent ID
asg_idstringYesAssignment ID
file_namestringYesFilename (e.g., program.py)
tokenstringYesCSRF authenticity token
cookiestringYesSession cookie

Return Value ​

javascript
{
  success: true,
  url: "https://www.gradescope.com/courses/.../submissions/..."
}

Or on error:

javascript
{
  success: false,
  error: "Error message"
}

How It Works ​

1. Create File Blob ​

javascript
const blob = new Blob([code], { type: 'text/x-python' });

2. Build FormData ​

javascript
const formData = new FormData();
formData.append('submission[files][]', blob, file_name);
formData.append('authenticity_token', token);

3. Submit to Gradescope ​

javascript
const response = await fetch(
  `https://www.gradescope.com/courses/${course_id}/assignments/${asg_id}/submissions`,
  {
    method: 'POST',
    headers: {
      'Cookie': cookie
    },
    body: formData
  }
);

Gradescope API Details ​

Submission URL ​

POST https://www.gradescope.com/courses/{course_id}/assignments/{asg_id}/submissions

Required Headers ​

HeaderValue
CookieSession cookie from Gradescope
Content-Typemultipart/form-data (set automatically)

Required Form Fields ​

FieldDescription
submission[files][]The code file
authenticity_tokenCSRF token

Getting Credentials ​

Authenticity Token ​

The CSRF token can be found:

  1. In the page source of any Gradescope form
  2. In form data when submitting manually
  3. In the <meta name="csrf-token"> tag

The session cookie is set when you log into Gradescope. It typically includes:

  • _gradescope_session
  • signed_token (sometimes)

Error Handling ​

Common Errors ​

ErrorCauseSolution
401 UnauthorizedInvalid/expired cookieRefresh credentials
403 ForbiddenInvalid CSRF tokenGet new token
404 Not FoundInvalid course/assignment IDCheck IDs
422 UnprocessableInvalid submission formatCheck file format

Example Error Handling ​

javascript
try {
  const result = await submitAssg(...);
  if (!result.success) {
    console.error('Submission failed:', result.error);
  }
} catch (error) {
  console.error('Network error:', error);
}

Security Considerations ​

  1. Never expose credentials — TOKEN and COOKIE are server-side only
  2. Use HTTPS — All Gradescope communication is encrypted
  3. Rotate credentials — Update when they expire
  4. Don't log credentials — Avoid logging sensitive values

Testing ​

Mock Submission ​

For testing without hitting Gradescope:

javascript
// In test environment
if (process.env.NODE_ENV === 'test') {
  return {
    success: true,
    url: 'https://gradescope.com/mock/submission'
  };
}

Verify Credentials ​

javascript
// Quick test that credentials work
const testResult = await submitAssg(
  'print("test")',
  process.env.COURSE_ID,
  '',
  'TEST_ASSIGNMENT_ID',
  'test.py',
  process.env.TOKEN,
  process.env.COOKIE
);

Released under the MIT License.