REST API
Documentation
- Nederlands
- English
File transfer
Files can be supplied in two different ways. Back in the step Order we briefly showed how to supply files as URLs when creating an order:
Files = new PrintApi.Input.Order.Item.FileList
{
Cover = "https://www.printapi.nl/sample-book-a5-cover.pdf",
Content = "https://www.printapi.nl/sample-book-a5-content.pdf",
}
Print API will then automatically download the files asynchronously. This usually happens within 5 minutes. Should a download fail for any reason, you'll receive an automatic error report per e-mail. If this is a suitable solution for your application, you can skip ahead to the next step.
Are your files not publicly available from a webserver? Then read on: you can also upload your files to the API after you've placed an order.
Alternative Uploading
Alternatively, you can upload your files after creating an order. Print API will generate a unique
uploadUrl
for each file you need to supply. You can then simply POST
each file
directly to its upload URL. These URLs will be in the API response of your order.
To enable uploads, you must first omit the Files
-object from your order data,
or set it to null
in its entirety: this will tell the API to wait for uploads. Our C#
library has a special function for uploading files:
// var order = await client.Orders.PostAsync(...);
var item = order.Items.First();
using (var file = File.OpenRead("files/123/content.pdf"))
{
await client.Files.PostAsync(item.Files.Content, file, FileType.PDF);
}
using (var file = File.OpenRead("files/123/cover.pdf"))
{
await client.Files.PostAsync(item.Files.Cover, file, FileType.PDF);
}
The file paths files/123.content.pdf
and files/123/cover.pdf
are obviously
fictional. You can customize this snippet to upload your own file(s). Note: the Cover
file is
only required for books, so if you're ordering something else, you only need to supply the
Content
file.
Ordering multiple items with unique files?
Our example snippet above assumes you always order one item, with the same file(s). Often, this won't be the case, and you'll order multiple items with unique files. To map the right files to the right items, it might help to store some metadata when creating your order:
Supplying metadata:var item1 = new PrintApi.Input.Order.Item
{
ProductId = "poster_a4_sta",
Quantity = 100,
Metadata = Newtonsoft.Json.JsonConvert.SerializeObject(new
{
File = @"D:\e7JkW4YVNE1qg6iRMBZ3.png"
})
};
You can use the metadata
-field to store your own data, like a file path or file ID. It's
just a text field, so JSON, XML or plain text are all fine. When uploading, you could use this field
to map each item back to its file:
foreach (var item in order.Items)
{
var metadata = Newtonsoft.Json.Linq.JObject.Parse(item.Metadata);
using (var file = File.OpenRead(metadata.Value<string>("File")))
{
await client.Files.PostAsync(item.Files.Content, file, FileType.PNG);
}
}