Uploading files
To support uploading files to EventsAir, the API exposes a separate, dedicated endpoint. This page contains the necessary information to take advantage of it.
Overview
The GraphQL specification does not define how file uploads should be handled and there is no standard implementation across GraphQL servers and clients. To ensure compatibility with the broadest range of client applications a separate API endpoint was created to handle file uploads:
https://api.eventsairtest.com/file
This implies that uploading files to EventsAir is a multi-step process:
- Upload the file through the endpoint mentioned above, whose response will contain a temporary file identifier.
- Use the temporary file identifier in a GraphQL mutation.
Request
The server expects a request with the following attributes:
- It must be authenticated like GraphQL requests, see the "Get an access token" guide for more information.
- The
Content-Type
header must be set tomultipart/form-data
. - The body must contain a single part named
file
which contains the file contents. - By default, files uploaded to EventsAir are limited to 15 MB in size.
Requests that don't comply with these requirements will not be processed.
Here are some examples to issue such a request.
- C#
- JavaScript/TypeScript
internal class Program
{
static async Task Main(string[] args)
{
var httpClient = new HttpClient
{
BaseAddress = new Uri("https://api.eventsairtest.com"),
};
var accessToken = "<access-token>";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var filePath = "<path-to-file>";
var content = new MultipartFormDataContent
{
{ new StreamContent(File.OpenRead(filePath)), "file", Path.GetFileName(filePath) }
};
var response = await httpClient.PostAsync("/file", content);
var data = await response.Content.ReadFromJsonAsync<FileUploadResponse>();
Console.WriteLine(data.TemporaryFileId);
}
}
internal class FileUploadResponse
{
public Guid TemporaryFileId { get; set; }
}
This sample uses the following npm packages:
const accessToken = '<access-token>'
const httpClient = axios.create({
baseURL: 'https://api.eventsairtest.com',
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
const filePath = '<path-to-file>'
const formData = new NodeFormData()
formData.append('file', fs.createReadStream(filePath), {
// This sets the "filename" attribute on the part automatically
filepath: filePath,
})
// Axios automatically transforms the `formData` instance into a multipart/form-data request
const response = await httpClient.post('/file', formData)
console.log(response.data.temporaryFileId)