Uploading an IFC file using the API endpoint

Hi Speckle Teams,

thanks a lot for creating this cool software.

I am not quite sure if I am asking this correctly, as I am new to speckle, but I would like to achieve the following:

I have an IFC file, which I would like to add to my stream - but not via the UI, but programmatically.
I know that Speckle offers an easy way to upload an IFC file via the Frontend, and this works fine.
Now I would like to do the same thing, but in my C# code. E.g. by either using one of the provided REST (or GraphQL) endpoints, or (even better) via some method offered somewhere in your packages or your speckle-sharp project.

I read in another community topic that there is already a REST endpoint that should be able to accept IFC files. So I tried the following

var requestUri = $"https://speckle.xyz/api/file/autodetect/d534066acf/main";
using (var multipartFormContent = new MultipartFormDataContent()) {
     var fileStreamContent = new StreamContent(File.OpenRead(@"HOTEL_archi.ifc"));
     fileStreamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
     multipartFormContent.Add(fileStreamContent, name: "file", fileName: @"HOTEL_archi.ifc");

     var client = new HttpClient();
     client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "Bearer 03e_TOKENINSERTEDHERE");
     var response = client.PostAsync(requestUri, multipartFormContent).GetAwaiter().GetResult();

     if (response.StatusCode == System.Net.HttpStatusCode.Created) {
         Console.WriteLine("OK, file was uploaded to speckle server.");

     } else {
         Console.WriteLine($"Error, file was not uploaded to speckle server. StatusCode: {response.StatusCode}");
     }
 }

Unfortunately, when I execute this code, I get a “FORBIDDEN” as response from the server.

I wasn’t able to find an endpoint for an IFC upload in the docs, so I am not quite sure if this is possible at all. And I wasn’t able to really figure out how the authorization token should look like exactly. Perhaps some more header is also missing in the request (e.g. User-Agent or stuff like this?)

Could you please advise me here, what the best way would be to upload an IFC file?

Thanks in advance,

Andreas

1 Like

Hi all,
just wanted to follow up on this - any ideas how I can upload an Ifc (either the file or as an object structure) to Speckle in my C# coding? Is there an API available to upload the whole file? Or do I need to somehow convert the Ifc (e.g. with the help of XBim) into Speckle objects - what would be the best way to achieve this?

Apologies @andreas.lennartz - your message slipped through the .net

There’s no need to prepare or convert IFC files. The upload to the API endpoint is all that is required.

Looking at your sample code, the endpoint is different.

I posted about the methods for binary uploads in answer to someone asking about attachments: API post comments with attached files - #7 by jonathon

The upload endpoint has a listener that will detect IFC as the upload format (crudely by extension), and the parser will progress from there to make a model with the uploaded file’s name.

Hi @jonathon ,

thanks for getting back to me, and pointing me in the right direction.

I think I was able to upload the IFC using the endpoint https://speckle.xyz/api/stream/{streamid}/blob - when I send the IFC file to this endpoint, I got as response:

{ 
 "uploadResults": [
  { 
    "blobId" : "220ed6f0d7",
    "fileName" : "HOTEL_archi.ifc",
    "uploadStatus" : 1,
    "fileSize" : 164829,
    "formKey":"file"}
  ]
}

So this is an improvement. Is there a way to see somehow the uploaded blob files?
When logging in at speckle.xyz, I wasn’t able to see any activities in my feed. No changes where shown in the UI.

However, in the other post you then used the uploaded file (an image) to add it to a comment using graphql.
Unfortunately, I don’t want to add the IFC file to a comment - I am not quite sure if this is even possible. (However, I tried, but without success)

You wrote that after uploading the IFC file the parser will progress from here to make a model. I would like to use the (converted) IFC file and create a commit for the stream of it.
I guess I need to do something here to trigger this - but I am not quite sure how to do this.
I tried to use the GrahpQL API to use the recently created blobId (ca0fbd7bb1) to connect/insert into a commit.

So I tried:

   $streamd: String!,
)
{ 
  commitCreate(
    commit: {
      streamId:$streamd,
      branchName: "main",
      objectId:"ca0fbd7bb1",
      message:"Test"
    }
  )
}

But without success. I got the Exception message "“Couldn’t find commit object”.
Could you tell me what I need to do in order to load the uploaded (and hopefully converted/parsed) Ifc blob into a commit?

Thanks for your support!

1 Like

I would also be interested in understanding this better :slight_smile:

@andreas.lennartz, I was misleading; the endpoint you were testing can work.

I think you have an error where you define your Authentication; it should be

client.DefaultRequestHeaders.Authorization = 
    new System.Net.Http.Headers.AuthenticationHeaderValue(
        "Bearer", 
        "03e_TOKENINSERTEDHERE"
    );

@jonathon
Hi Jonathan,
thanks for pointing this out. I actually were using it like you suggested already in my code (I took it from the article that you linked in your first reply).

The thing still is that I get a successful status code as response from the server - including a BlobId for the uploaded Ifc. So I receive this as a response:

 "uploadResults": [
  { 
    "blobId" : "220ed6f0d7",
    "fileName" : "HOTEL_archi.ifc",
    "uploadStatus" : 1,
    "fileSize" : 164829,
    "formKey":"file"}
  ]
}

But what’s next?
No uploaded/converted IFC data/file shows up in my stream. When looking at the UI, I wasn’t able to see any activities in my feed. No changes where shown in the UI.

You wrote that after uploading the IFC file the parser will progress from here to make a model. Does this happen automatically? Or do I need to take some actions that the uploaded IFC is converted into the Speckle data model and written into my stream?

Appreciate your time and your support!

This may be my fault - what do you get from the original endpoint code you posted in the first post? But with the corrected auth token

Wait, I just re-read your questions, and saw that I understood it wrong… I will test it out with the original endpoint.

1 Like

@jonathon
Using the endpoint

https://speckle.xyz/api/file/autodetect/{streamid}/main

and the correct authentication header now worked!!!
The IFC was uploaded/converted & inserted as a new commit, which is awesome.

Interestingly, I get more or less the same response from the server as for the other endpoint.
Thanks a lot for your support!

By the way, both endpoint do not appear in the REST API documentation - is there some kind of overview/source code that shows the currently implemented endpoints?

No, and this is on us - we will be refreshing everything as part of the FE2 launch