0

Upload an image

by
Published Oct 17, 2025

Upload an image to your ReadMe project.

Script readme Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Upload an image
4
 * Upload an image to your ReadMe project.
5
 */
6
export async function main(auth: RT.Readme, body: Body, resize_height?: string | undefined) {
7
	const url = new URL(`https://api.readme.com/v2/images`)
8
	for (const [k, v] of [['resize_height', resize_height]]) {
9
		if (v !== undefined && v !== '') {
10
			url.searchParams.append(k, v)
11
		}
12
	}
13
	const formData = new FormData()
14
	for (const [k, v] of Object.entries(body)) {
15
		if (v !== undefined && v !== '') {
16
			formData.append(k, String(v))
17
		}
18
	}
19
	const response = await fetch(url, {
20
		method: 'POST',
21
		headers: {
22
			Authorization: 'Bearer ' + auth.apiKey
23
		},
24
		body: formData
25
	})
26
	if (!response.ok) {
27
		const text = await response.text()
28
		throw new Error(`${response.status} ${text}`)
29
	}
30
	return await response.json()
31
}
32

33
/* eslint-disable */
34
/**
35
 * This file was automatically generated by json-schema-to-typescript.
36
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
37
 * and run json-schema-to-typescript to regenerate this file.
38
 */
39

40
export interface Body {
41
	file?: unknown
42
}
43