└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ## An example of how to pin a file to IPFS with Pinata in a JavaScript app 2 | 3 | If you're like me and had no luck finding any examples or code to show how this is done, then hopefully this will help you. 4 | 5 | ### Pinning a string 6 | 7 | ```javascript 8 | async function save() { 9 | let ipfs = await IPFS.create({ 10 | url: "https://api.pinata.cloud/psa", 11 | repo: 'file-path' + Math.random() 12 | }) 13 | const { cid } = await ipfs.add('hello world') 14 | const url = `https://gateway.pinata.cloud/ipfs/${cid.string}` 15 | console.log(url) 16 | } 17 | ``` 18 | 19 | ### Pinning a file 20 | 21 | ```javascript 22 | async function onChange(e) { 23 | const file = e.target.files[0] 24 | let ipfs = await IPFS.create({ 25 | url: "https://api.pinata.cloud/psa", 26 | repo: 'file-path' + Math.random() 27 | }) 28 | const { cid } = await ipfs.add(file) 29 | const url = `https://gateway.pinata.cloud/ipfs/${cid.string}` 30 | console.log(url) 31 | } 32 | ``` --------------------------------------------------------------------------------