Goal: upload a PDF to Shopify Files via API for use on product pages. Initial approach used REST POST to /admin/api/2023-10/files.json with multipart form data, resulting in 406 Not Acceptable; there is no REST /files endpoint.
Updated approach (confirmed working):
Use Admin GraphQL stagedUploadsCreate to generate a staged upload target with resource=FILE, filename, and mimeType=application/pdf.
Upload the file with a PUT request to stagedTargets.url returned in step 1. Send the raw file bytes to that URL; do not use multipart form data or extra parameters in headers/body.
On 200 OK from the upload, call the Admin GraphQL fileCreate mutation to add the file to the Files section, passing originalSource as the staged upload URL from step 1 without query parameters.
Clarifications: The upload step is PUT (not POST). The earlier confusion about passing parameters in body/headers does not apply for FILE; upload directly to the provided URL. A Postman example was referenced; documentation for FILE uploads is limited.
Status: Resolved with a working 3-step method above.
Hi all. I’m using a Custom Data field to allow to upload a PDF file each product on their Shopify store. The file is going to be downloadable from each product page.
I would like to write a function in C# to upload files via the API but I can’t understand what the correct procedure is.
This is my code example (it doesn’t work):
public async Task UploadFile(byte[] fileContent, string fileName)
{
using (var client = new HttpClient())
{
var requestUri = $"https://{_shopifyStoreUrl}/admin/api/2023-10/files.json";
client.DefaultRequestHeaders.Add("X-Shopify-Access-Token", _accessToken);
var content = new MultipartFormDataContent();
var fileContentContent = new ByteArrayContent(fileContent);
fileContentContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
FileName = $"\"{fileName}\""
};
content.Add(fileContentContent);
var response = await client.PostAsync(requestUri, content);
response.EnsureSuccessStatusCode();
}
}
Can you help me understand where I’m wrong?
The response is: “StatusCode: 406, ReasonPhrase: ‘Not Acceptable’”
I can’t speak to the C# code specifically but can point you in the right direction. I don’t think there’s a /files endpoint - but you should be able to create a file with this (GraphQL) mutation.
Hi,
This article is missing the example in “UPLOAD SECTION” of when a generic “FILE” type must be uploaded (in my case a PDF) so I can’t understand if the request must be of type POST or PUT and where (and how) the parameters should be passed.
Can you tell me if for a generic FILE (pdf) type the request must be POST OR PUT and how the parameters should be passed?
In the first step I followed the instructions by putting the type “FILE” and I received the correct parameters but when it says to upload I tried various ways without success.
Upload the file (A PUT request to data.stagedUploadsCreate.stagedTargets.url from step 1. If you want to test in Postman here’s an example screenshot.
If you get a 200 OK from step 2, then you can use the fileCreate mutation to add the file to the ‘files’ section of the admin. You only need to pass the originalSource param, which is the data.stagedUploadsCreate.stagedTargets.url value from step one without any of the params.