☑ How to download a project with the REST API

hey guys,
I was looking into the documentation about the new rest API, but I still have no clue where to start :sweat_smile:
I never worked with REST before, so I am a little bit lost with your documentation.

It would be nice to have some small walk-through how to setup a simple task with the api, like downloading a web build.
My goal is to be able to download our apps into a grunt project to further process them automatically, but I already have troubles to access the project via curl. I tried to access one of our projects with the following command from a linux shell:

curl -H "Authorization: OrganisationName AuthCode" https://playcanvas.com/api/apps/ProjectID

But I always get the response, that the app cannot be found. Isn’t the Project ID the number in the url if you are on project overview page? Like:
https://playcanvas.com/project/ProjectID/overview/

I would appreciate any poke into the right direction :smiley:

greetings,

martin

So the REST API consists of a few endpoints that we provide that allow you to talk to PlayCanvas and do various things. One of those things is to download a web build. This is documented here:

http://developer.playcanvas.com/en/user-manual/api/download-app/

So the endpoint you have to use is https://playcanvas.com/api/apps/download and the method you use must be POST. More about methods here ( http://www.restapitutorial.com/lessons/httpmethods.html ). When you use POST you must pass data in the form of JSON. The parameters you need to pass are documented in the endpoint’s documentation page.

Every time you call an endpoint you have to pass some Authorization details in the header. This must be in the form of Bearer access_token where access_token is your personal token generated from the account page. So this header you use in your example is wrong:

Authorization: OrganisationName AuthCode

It should be

Authorization: Bearer access_token

So say we want to download a web build for project 99999 and we want to name the build My App. My example access token is aNPXAa2JZGduUsynoIJwt5T2csxAfjB8. With curl we could do:

curl -H "Authorization: Bearer aNPXAa2JZGduUsynoIJwt5T2csxAfjB8" -H "Content-Type: application/json" -X POST -d '{"project_id": 99999, "name": "My App"}' https://playcanvas.com/api/apps/download

This will return something like

{
  "status": "running",
  "messages": [],
  "created_at": "2016-10-12T15:39:11.582000",
  "modified_at": "2016-10-12T15:39:11.582000",
  "data": {
    "concatenate": false,
    "target": "web",
    "owner_id": 152329,
    "scenes": [
      "36532325"
    ],
    "project_id": 99999,
    "name": "My App"
  },
  "id": 2123212
}

The request we made started a download job which takes some time to complete (usually a few seconds). So we need to wait until the job is completed before we can download our build. The request above returned the job details as the result. You can poll the job by id using our REST API until the job has status: complete. For example in this case the job has id: 2123212 so we can poll it with curl like so:

curl -H "Authorization: Bearer aNPXAa2JZGduUsynoIJwt5T2csxAfjB8"  https://playcanvas.com/api/jobs/2123212

This will return back the job data so let’s assume it’s completed by now so it would look something like this:

{
  "status": "complete",
  "messages": [],
  "created_at": "2016-10-12T15:39:11.582000",
  "modified_at": "2016-10-12T15:39:11.582000",
  "data": {
    "concatenate": false,
    "name": "My App",
    "scenes": [
      "36532325"
    ],
    "download_url": "url_to_zip_file_here",
    "target": "web",
    "project_id": 99999,
    "owner_id": 152329
  },
  "id": 2123212
}

When the job is complete you’ll notice it has a download_url in the data. You can use that URL to download your build.

hey vajos,

thanks a lot for that detailed answer, it helped a lot.
With your explanation that stuff is way clearer than before :>

I created a small grunt task to download a zip of our projects now, but I still have one tiny problem remaining. For some reason I cannot get the task to concatenate the scripts.

I added the optional parameter to the request, but the returned job is always stating, that concatenate is still on false.
-X POST -d '{"project_id": 99999, "name": "My App", "script_concatenate" : true }'

Do I miss something again?

Greetings,

Martin

Ah sorry it looks like the parameter name should be scripts_concatenate not script_concatenate. I’ll update the docs.

Ah, I tried a dozen combinations, except that one…
Now we have a one click deployment :heart_eyes:

Thanks again for the quick help :+1:

Greetings,

Martin

1 Like