Create Certificate

Create an Origin CA certificate. Use your Origin CA Key as your User Service Key when calling this endpoint ([see above](#requests)).

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create Certificate
8
 * Create an Origin CA certificate. Use your Origin CA Key as your User Service Key when calling this endpoint ([see above](#requests)).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  body: {
13
    csr?: string;
14
    hostnames?: unknown[];
15
    request_type?: "origin-rsa" | "origin-ecc" | "keyless-certificate";
16
    requested_validity?: 7 | 30 | 90 | 365 | 730 | 1095 | 5475;
17
    [k: string]: unknown;
18
  }
19
) {
20
  const url = new URL(`https://api.cloudflare.com/client/v4/certificates`);
21

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