Images
Images
Uploading images for events is a three step process, first you request single use upload credentials from the API, then upload the image to a temporary location, then you use the image key in the saveEvent mutation to attach the image to the event.
The following example demontrates how to set the main image for an event to be the same image as a public URL.
import fetch, { Headers } from 'node-fetch';
const execute = async () => {
try {
// Get temp upload credentials
const queryGetTempImage = `
mutation {
viewer(token: "your-token-here") {
generateFileUploadLink(clientId: 6080, contentType: "image/png") {
key
url
}
}
}
`;
const response = await fetch('https://api.moshtix.com/v1/graphql', {
method: 'POST',
body: JSON.stringify({ query: queryGetTempImage }),
headers: { 'Content-Type': 'application/json' },
});
const tempUploadDetails = await response.json();
const tempKey = tempUploadDetails.data.viewer.generateFileUploadLink.key;
const tempUrl = tempUploadDetails.data.viewer.generateFileUploadLink.url;
// Download image from URL
const downloadResponse = await fetch(
'https://static.moshtix.com.au/uploads/df4ef274-4fd8-4eff-8263-d1b744bd51adx600x600',
{
method: 'GET',
encoding: null, // This is actually important, or the image string will be encoded to the default encoding
},
);
// Upload image to temp location
const imageUploadResponse = await fetch(tempUrl, {
method: 'PUT',
headers: new Headers({
'Content-Type': downloadResponse.headers.get('Content-Type'),
'Content-Length': downloadResponse.headers.get('Content-Length'),
}),
body: downloadResponse.body,
});
if (imageUploadResponse.status !== 200) {
throw new Error(`Upload failed with error ${imageUploadResponse.status} - ${imageUploadResponse.statusText}.}`);
}
// Attach image to event as main image
const queryAttachImageToEvent = `
mutation {
viewer(token: "your-token-here") {
saveEvent(event: {
id: 132422,
images: {
items: [
{
type: "MAIN",
uploadKey: "${tempKey}"
}
]
}
}) {
id
images {
items {
url
}
}
}
}
}
`;
const saveEventResponse = await fetch('https://api.moshtix.com/v1/graphql', {
method: 'POST',
body: JSON.stringify({ query: queryAttachImageToEvent }),
headers: { 'Content-Type': 'application/json' },
});
console.log(JSON.stringify(await saveEventResponse.json()));
} catch (err) {
console.log(err);
}
};
execute();