Last active
          November 17, 2022 17:52 
        
      - 
      
- 
        Save stevedylandev/2f23f75dd53a061d8f39c886c880f1cf to your computer and use it in GitHub Desktop. 
    A function that will upload to Pinata through a buffer
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const axios = require("axios"); | |
| const axiosRetry = require("axios-retry"); | |
| const FormData = require("form-data"); | |
| const jwt = `Bearer PASTE_YOUR_JWT` | |
| const uploadToPinata = async (sourceUrl) => { | |
| const axiosInstance = axios.create(); | |
| axiosRetry(axiosInstance, { retries: 5 }); | |
| const data = new FormData(); | |
| const response = await axiosInstance(sourceUrl, { | |
| method: "GET", | |
| responseType: "stream", | |
| }); | |
| data.append(`file`, response.data); | |
| try { | |
| const res = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", data, { | |
| maxBodyLength: "Infinity", | |
| headers: { | |
| 'Content-Type': `multipart/form-data; boundary=${data._boundary}`, | |
| 'Authorization': jwt | |
| } | |
| }); | |
| console.log(res.data); | |
| } catch (error) { | |
| console.log(error) | |
| } | |
| }; | |
| uploadToPinata("https://example.com/1.png") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment