The PythonAnywhere API (beta)

Disclaimer

Warning: our API is new and in beta and not officially supported, and may change at any time, and it is not to be relied upon, and may cause unpredictable growth of extra ears. Extra ears not guaranteed.

Getting started and Authentication

The PythonAnywhere API uses token-based authentication. You can get your token from your Account page on the API Token tab.

It's used in a header called Authorization, and the value is encoded as the string "Token", followed by a space, followed by your token, like this:

'Authorization': 'Token {}'.format(token)

For example, this code using the requests module would get the details of your CPU usage on PythonAnywhere; you would just need to change the three variables at the top to match your actual username, your API token, and the correct host:

  • www.pythonanywhere.com if your account is on our US-based system.
  • eu.pythonanywhere.com if your account is on our EU-based system.
import requests
username = 'your username'
token = 'your token'
host = 'your host'

response = requests.get(
    'https://{host}/api/v0/user/{username}/cpu/'.format(
        host=host, username=username
    ),
    headers={'Authorization': 'Token {token}'.format(token=token)}
)
if response.status_code == 200:
    print('CPU quota info:')
    print(response.content)
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.content))

Once you've generated your token, you can copy and paste it for use in your scripts. You can also access it at any time from PythonAnywhere consoles, webapps and tasks in a pre-populated environment variable, $API_TOKEN.

You will need to reload your webapp and start new consoles for this environment variable to be in place.

Endpoints

All endpoints are hosted at https://www.pythonanywhere.com/ or https://eu.pythonanywhere.com/ depending on where your account is registered.

Rate-limits

Each endpoint has a 40 requests per minute rate limit, apart from the send_input endpoint on consoles, which is 120 requests per minute.

Always_On

/api/v0/user/{username}/always_on/

Method Description Parameters
GET List all of your always-on tasks (no parameters)
POST Create and start a new always-on task command, description, enabled

/api/v0/user/{username}/always_on/{id}/

Method Description Parameters
GET Return information about an always-on task. (no parameters)
PUT Endpoints for always-on tasks command, description, enabled
PATCH Endpoints for always-on tasks command, description, enabled
DELETE Stop and delete an always-on task (no parameters)

/api/v0/user/{username}/always_on/{id}/restart/

Method Description Parameters
POST Endpoints for always-on tasks command, description, enabled

Consoles

/api/v0/user/{username}/consoles/

Method Description Parameters
GET List all your consoles (no parameters)
POST Create a new console object (NB does not actually start the process. Only connecting to the console in a browser will do that). executable, arguments, working_directory

/api/v0/user/{username}/consoles/shared_with_you/

Method Description Parameters
GET View consoles shared with you. (no parameters)

/api/v0/user/{username}/consoles/{id}/

Method Description Parameters
GET Return information about a console instance. (no parameters)
DELETE Kill a console. (no parameters)

/api/v0/user/{username}/consoles/{id}/get_latest_output/

Method Description Parameters
GET Get the most recent output from the console (approximately 500 characters). (no parameters)

/api/v0/user/{username}/consoles/{id}/send_input/

Method Description Parameters
POST "type" into the console. Add a "\n" for return. POST parameter: input

Cpu

/api/v0/user/{username}/cpu/

Method Description Parameters
GET Returns information about cpu usage in json format:
{
    "daily_cpu_limit_seconds": <int>,
    "next_reset_time": <isoformat>,
    "daily_cpu_total_usage_seconds": <float>
}
(no parameters)

Default_Python3_Version

/api/v0/user/{username}/default_python3_version/

Method Description Parameters
GET Returns information about user's current and available default Python 3 version in json format:
{
    "default_python3_version": <str>,
    "available_python3_versions": [<str>],
}
(no parameters)
PATCH Sets default Python 3 version for user. (no parameters)

Default_Python_Version

/api/v0/user/{username}/default_python_version/

Method Description Parameters
GET Returns information about user's current and available default Python version in json format:
{
    "default_python_version": <str>,
    "available_python_versions": [<str>],
}
(no parameters)
PATCH Sets default Python version for user. (no parameters)

Default_Save_And_Run_Python_Version

/api/v0/user/{username}/default_save_and_run_python_version/

Method Description Parameters
GET Returns information about user's current and available Python version used for the "Run" button in the editor, in json format:
{
    "default_save_and_run_python_version": <str>,
    "available_python_versions": [<str>],
}
(no parameters)
PATCH Sets Python version used for the "Run" button in the editor. (no parameters)

Files

/api/v0/user/{username}/files/path{path}

Method Description Parameters
GET (no parameters)
POST Uploads a file to the specified file path. Contents should be in a multipart-encoded file with the name "content". The attached filename is ignored. If the directories in the given path do not exist, they will be created. Any file already present at the specified path will be overwritten. Returns 201 on success if a file has been created, or 200 if an existing file has been updated. (no parameters)
DELETE Deletes the file at the specified path. This method can be used to delete log files that are not longer required. Returns 204 on success. (no parameters)

/api/v0/user/{username}/files/sharing/

Method Description Parameters
POST Start sharing a file. Returns 201 on success, or 200 if file was already shared. POST parameter: path

/api/v0/user/{username}/files/sharing/?path={path}

Method Description Parameters
GET Check sharing status for a path. Returns 404 if path not currently shared. Query parameter: path
DELETE Stop sharing a path. Returns 204 on successful unshare. Query parameter: path

/api/v0/user/{username}/files/tree/?path={path}

Method Description Parameters
GET Returns a list of the contents of a directory, and its subdirectories as a list. Paths ending in slash/ represent directories. Limited to 1000 results. Query parameter: path

Schedule

/api/v0/user/{username}/schedule/

Method Description Parameters
GET List all of your scheduled tasks (no parameters)
POST Create a new scheduled task command, enabled, interval, hour, minute, description

/api/v0/user/{username}/schedule/{id}/

Method Description Parameters
GET Return information about a scheduled task. (no parameters)
PUT Endpoints for scheduled tasks command, enabled, interval, hour, minute, description
PATCH Endpoints for scheduled tasks command, enabled, interval, hour, minute, description
DELETE Delete an scheduled task (no parameters)

Students

/api/v0/user/{username}/students/

Method Description Parameters
GET Returns a list of students of the current user
{
    "students": [
        {"username": <string>},
        {"username": <string>},
        ...
    ]
}
(no parameters)

/api/v0/user/{username}/students/{student}/

Method Description Parameters
DELETE (no parameters)

System_Image

/api/v0/user/{username}/system_image/

Method Description Parameters
GET Returns information about user's current and available system images in json format:
{
    "system_image": <str>,
    "available_system_images": [<str>],
}
(no parameters)
PATCH Sets system image for user. (no parameters)

Webapps

/api/v0/user/{username}/webapps/

Method Description Parameters
GET List all webapps (no parameters)
POST Create a new webapp with manual configuration. Use (for example) "python36" to specify Python 3.6. POST parameters: domain_name, python_version

/api/v0/user/{username}/webapps/{domain_name}/

Method Description Parameters
GET Return information about a web app's configuration (no parameters)
PUT Modify configuration of a web app. (NB a reload is usually required to apply changes). python_version, source_directory, virtualen v_path, force_https, password_protection_enabled, password_protection_username, password_protection_password
PATCH Modify configuration of a web app. (NB a reload is usually required to apply changes). python_version, source_directory, virtual env_path, force_https, password_protection_enabled, password_protection_username, password_protection_password
DELETE Delete the webapp. This will take the site offline. Config is backed up in /var/www, and your code is not touched. (no parameters)

/api/v0/user/{username}/webapps/{domain_name}/disable/

Method Description Parameters
POST Disable the webapp. POST parameters: none

/api/v0/user/{username}/webapps/{domain_name}/enable/

Method Description Parameters
POST Enable the webapp. POST parameters: none

/api/v0/user/{username}/webapps/{domain_name}/reload/

Method Description Parameters
POST Reload the webapp to reflect changes to configuration and/or source code on disk. POST parameters: none

/api/v0/user/{username}/webapps/{domain_name}/ssl/

Method Description Parameters
GET Get and set TLS/HTTPS info. POST parameters to the right are incorrect, use `cert` and `private_key` when posting. (no parameters)
POST Get and set TLS/HTTPS info. POST parameters to the right are incorrect, use `cert` and `private_key` when posting. python_version, source_directory, virtualenv_path, force_https, password_protection_enabled, password_protection_username, password_protection_passw ord
DELETE Get and set TLS/HTTPS info. POST parameters to the right are incorrect, use `cert` and `private_key` when posting. (no parameters)

/api/v0/user/{username}/webapps/{domain_name}/static_files/

Method Description Parameters
GET List all the static files mappings for a domain. (no parameters)
POST Create a new static files mapping. (webapp restart required) url, path

/api/v0/user/{username}/webapps/{domain_name}/static_files/{id}/

Method Description Parameters
GET Get URL and path of a particular mapping. (no parameters)
PUT Modify a static files mapping. (webapp restart required) url, path
PATCH Modify a static files mapping. (webapp restart required) url, path
DELETE Remove a static files mapping. (webapp restart required) (no parameters)

/api/v0/user/{username}/webapps/{domain_name}/static_headers/

Method Description Parameters
GET List all the static headers for a domain. (no parameters)
POST Create a new static header. (webapp restart required) url, name, value

/api/v0/user/{username}/webapps/{domain_name}/static_headers/{id}/

Method Description Parameters
GET Get URL, name and value of a particular header. (no parameters)
PUT Modify a static header. (webapp restart required) url, name, value
PATCH Modify a static header. (webapp restart required) url, name, value
DELETE Remove a static header. (webapp restart required) (no parameters)