Skip to main content

Upload Image

Upload a base64-encoded image. The image is cached and a moderation check is performed. Returns a CachedImage with the hosted URL and safety score.

Mutation Structure​

mutation UploadImage($image: UploadImageInput!) {
uploadImage(image: $image) {
images {
url
original_url
safe
score
model
created_at
}
}
}

Variables​

The mutation takes an image argument with the UploadImageInput type:

FieldTypeRequiredDescription
dataStringYesBase64-encoded image data
filenameStringYesFilename with extension
contentTypeStringYesMIME type (e.g. "image/png")
{
"image": {
"data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
"filename": "pixel.png",
"contentType": "image/png"
}
}

Response Fields​

Returns UploadImageFromUrlOutput containing an images array of CachedImage objects:

FieldTypeDescription
images[CachedImage!]!Array of cached image results
images[].urlString!Hosted image URL
images[].original_urlString!Original source URL
images[].safeBoolean!Whether the image passed moderation
images[].scorejsonbModeration safety score details
images[].modelStringModeration model used
images[].created_attimestamptz!Upload timestamp

Expected Response​

{
"data": {
"uploadImage": {
"images": [
{
"url": "https://cdn.example.com/images/abc123.png",
"original_url": "https://cdn.example.com/images/abc123.png",
"safe": true,
"score": null,
"model": null,
"created_at": "2024-01-15T10:30:00Z"
}
]
}
}
}

Use Cases​

File Input Upload​

Handle file uploads from a form:

import { GraphQLClient } from 'graphql-request'
import { API_URL_PROD } from '@0xintuition/graphql'

const client = new GraphQLClient(API_URL_PROD)

async function uploadFileInput(file: File) {
const buffer = await file.arrayBuffer()
const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)))

const mutation = `
mutation UploadImage($image: UploadImageInput!) {
uploadImage(image: $image) {
images {
url
safe
}
}
}
`

const result = await client.request(mutation, {
image: {
data: base64,
filename: file.name,
contentType: file.type
}
})

return result.uploadImage.images[0]
}