Get Asset File from the REST API

Hello everyone,

I’ll get straight to the point, I’m trying to get an asset file from a private Playcanvas project I have access to. For this purpose I’m trying to use the REST API, specifically the route documented here REST API GET ASSET FILE , using the following code:

       //Headers definition: Authorization token is needed to use the Playcanvas API
        myHeaders = new Headers({
            "Authorization": `Bearer ${Constants.API_TOKEN}`
        });
        //Playcanvas REST API: GET ASSET FILE
        let myReq = new Request(`https://playcanvas.com/api/assets/${modelAsset.id}/file?branchId=${Constants.BRANCH_ID}`);
        
        await fetch(myReq, { 
            headers: myHeaders ,
            method: 'GET'
        }).then(function(response){});

But the response to this is an asset not found 404 error, which is weird cause if I use the route to get the asset details like this:
let myReq = new Request(`https://playcanvas.com/api/assets/${modelAsset.id}?branchId=${Constants.BRANCH_ID}`);

This returns the assets details correctly with a 200 code. I have also tried to use a route like this, from another forum post:

let myReq = new Request(`https://playcanvas.com/api/assets/${modelAsset.id}/file/${modelAsset.name}?branchId=${Constants.BRANCH_ID}`);

This previously worked, but now throws a CORS error.

Can this be related to the project being private?

I would appreciate any help.

Where are you running the request from? Commandline locally on a PC?

From the same Playcanvas private project, the goal was to create a tool to update the files dropped in the Playcanvas editor to our own server, for which I previously need to get the asset file using the REST API. There are probably other ways to achieve this but first I would like to know why this is no longer works.

When did it used to work? We may have made some changes to the routing/caching of the launch tab

As far as I remember, it worked in mid-October 2021, the last time I tried it with the same code.

1 Like

@yak32 / @jpaulo Could this be related to the changes we’ve made during that time?

@Mediotaku Around that time, we made several changes to increase the speed and stability of the launch and asset loading that could have affected this hence why this may no longer work

Out of interest, what is the use case for getting the model asset via the REST API in the launch tab?

What happens when you use https://launch.playcanvas.com/api/assets instead? Does that get around the CORs?

We are creating a mobile game with a heavy load of model assets, so we have decided to dynamically download them from our own server to keep the initial load time low. But for the convenience of the 3D artists, we are creating a tool within Playcanvas to allow them to directly upload these models with some metadata entered. The workflow would be for them to drop some models into the editor and open the launch tab, where the models will be displayed in a list along with a metadata form to fill in and an upload button, which calls the REST API to download the actual asset file and send it subsequently to our server.

Ah, I see. For the moment, it feels like it would be better if the remote server did the REST API call instead of the launch tab and the launch tab would send the asset id with the meta data to the server on ‘upload’

Is the mobile game in PlayCanvas or a native app out of interest?

This seems to do the trick, thank you very much!

1 Like

Bear in mind, this is ‘private’ API so still subject to change :sweat_smile: We are looking this a bit and will create a ticket on this as we do want to explore the use of REST APIs usage in the launch tab bit more

1 Like

Thank you for the advice.
Everything is in Playcanvas by the way, game included.

In which case, could you have the asset(s) not preload and then use the asset registry to load/unload the assets when needed instead of loading by URL?

Or am I missing some nuance/context with your game?

Well, the thing is, we have a build size limit of 10MB, so we thought it best to have assets downloading on demand in anticipation of future content addition.

Oh, you have an upload ZIP build size limit of 10MB to wherever you are publishing?

1 Like

Unfortunately, yes, that is the nuance.

1 Like

Created ticket: https://github.com/playcanvas/editor/issues/685

2 Likes

Thinking about it, the other thing you could do which will be more robust would be to get the asset file via the asset registry URL instead

https://developer.playcanvas.com/api/pc.Asset.html#getFileUrl

You can download the file via the URL and use that to upload instead of the REST API?

3 Likes

Well, I could fetch the asset file like this:

let myReq = new Request(window.location.origin + modelAsset.getFileUrl());
        
        await fetch(myReq, { 
            headers: myHeaders,
            method: 'GET'
        }).then(function(response){});

as .getFileUrl() returns a relative path in this case. Is this what you mean?