0

Add redirect/rewrite rules

by
Published Oct 17, 2025

Add redirect/rewrite rules to the service with the provided ID.

Script render Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Render = {
3
	apiKey: string
4
}
5
/**
6
 * Add redirect/rewrite rules
7
 * Add redirect/rewrite rules to the service with the provided ID.
8

9
 */
10
export async function main(
11
	auth: Render,
12
	serviceId: string,
13
	body: {
14
		type: 'redirect' | 'rewrite'
15
		source: string
16
		destination: string
17
		priority?: number
18
	}
19
) {
20
	const url = new URL(`https://api.render.com/v1/services/${serviceId}/routes`)
21

22
	const response = await fetch(url, {
23
		method: 'POST',
24
		headers: {
25
			'Content-Type': 'application/json',
26
			Authorization: 'Bearer ' + auth.apiKey
27
		},
28
		body: JSON.stringify(body)
29
	})
30
	if (!response.ok) {
31
		const text = await response.text()
32
		throw new Error(`${response.status} ${text}`)
33
	}
34
	return await response.json()
35
}
36