Forums

Using PythonAnywhere API

I have created a webapp in order to use the API. I would like to download zip files that I am making in my /home/myname folder and then delete them from PythonAnywhere to make room for more zip files that a Python program on PythonAnywhere is generating.

I simply do not understand how to use the API. From the documentation I think it is okay to put

import requests
requestobj = requests.get("https://www.pythonanywhere.com/api/v0/user/{username}/files/tree/?path={path}".format(username=myusername, path="/home/myusername"))

But this does not yield anything. What do I need to do?

Also, is it possible to download a file through the API? What about deleting a file?

It looks like you're not setting the authorization token header in your request -- if you check the status_code field on your requestobj variable, you'll probably see that it's set to 401, which means "not authorized". There's some quick-and-simple code showing how to set the token on the API tab of the Accounts page -- I'll put a copy of that on the API help page later on today, too, because it should be in both places.

Unfortunately there's no download/delete file functionality in the API yet. That's something we should definitely add before it leaves beta status.

We have now added an API endpoint for files where you can upload, download or delete files. Details are of the API are here

import requests username = 'adil1919ster' token = '81fd31cd450d2cd82158f053e025bd0d4ca4da02'

response = requests.get('https://www.pythonanywhere.com/api/v0 user/{username}/cpu/'.format(username=adil1919ster), 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))

you just showed your token to everyone

Hi there,

I know this topic is old but has there been any progress in adding the download feature through the API for a zip file?

Thanks in advance

No, we haven't worked on that yet.