0

Update an API definition

by
Published Oct 17, 2025

Updates an API definition in the API Reference section of your ReadMe project. >📘 > This route is only available to projects that are using [ReadMe Refactored](https://docs.readme.com/main/docs/welcome-to-readme-refactored).

Script readme Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Update an API definition
4
 * Updates an API definition in the API Reference section of your ReadMe project.
5

6
>📘
7
> This route is only available to projects that are using [ReadMe Refactored](https://docs.readme.com/main/docs/welcome-to-readme-refactored).
8
 */
9
export async function main(auth: RT.Readme, filename: string, branch: string, body: Body) {
10
	const url = new URL(`https://api.readme.com/v2/branches/${branch}/apis/${filename}`)
11

12
	const formData = new FormData()
13
	for (const [k, v] of Object.entries(body)) {
14
		if (v !== undefined && v !== '') {
15
			formData.append(k, String(v))
16
		}
17
	}
18
	const response = await fetch(url, {
19
		method: 'PUT',
20
		headers: {
21
			Authorization: 'Bearer ' + auth.apiKey
22
		},
23
		body: formData
24
	})
25
	if (!response.ok) {
26
		const text = await response.text()
27
		throw new Error(`${response.status} ${text}`)
28
	}
29
	return await response.json()
30
}
31

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

39
/**
40
 * The API definition to upload. We provide full support for OpenAPI 3.x and Swagger 2.0 and experimental support for Postman collections.
41
 */
42
export interface Body {
43
	/**
44
	 * The API definition.
45
	 */
46
	schema?: {
47
		[k: string]: unknown
48
	}
49
	/**
50
	 * The source that the API definition is being uploaded through.
51
	 */
52
	upload_source?: string
53
	/**
54
	 * The URL where the API definition is hosted.
55
	 */
56
	url?: {
57
		[k: string]: unknown
58
	}
59
}
60