-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-file.ts
48 lines (42 loc) · 1.07 KB
/
upload-file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
async function main() {
try {
// Construct a file according to the Web API
const file = new File(["Hello World!"], "hello.txt");
// Create form data and attach the file
const data = new FormData();
data.append("file", file);
// Optional: Attach other info about the file
// Custom name and keyvalues
// const metadata = JSON.stringify({
// name: "My cool file",
// keyvalues: {
// env: "prod",
// user: "sudo",
// },
// });
// data.append("pinataMetadata", metadata);
// Change CID version or add to a Group
// const options = JSON.stringify({
// cidVersion: 1,
// groupId: "my-group-id",
// });
// data.append("pinataOptions", options);
// Upload the file
const uploadRequest = await fetch(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PINATA_JWT}`,
},
body: data,
},
);
// Parse the response and log it out
const upload = await uploadRequest.json();
console.log(upload);
} catch (error) {
console.log(error);
}
}
main();