Last active
          September 27, 2022 01:00 
        
      - 
      
- 
        Save stevedylandev/6a0c1b73f30a35304dc112f06b85125c to your computer and use it in GitHub Desktop. 
    A function that will let users get the short link of a pin
  
        
  
    
      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'); | |
| let pinIds = [] | |
| //Use your JWT from app.pinata.cloud/keys | |
| const JWT = `Bearer PASTE_YOUR_JWT` | |
| //replace with your own dedicated gateway domain, this is an example | |
| const GATEWAY_DOMAIN = `somegatewayexample.mypinata.cloud` | |
| //This function will return the shortened link after we get the file ID | |
| const shortenPin = (contentId) => { | |
| const url = `https://api.pinata.cloud/v2/pins/${contentId}/shorten`; | |
| let data = JSON.stringify({ | |
| "domain": GATEWAY_DOMAIN | |
| }) | |
| var config = { | |
| method: 'post', | |
| url: url, | |
| headers: { | |
| 'Authorization': JWT, | |
| 'Content-Type': 'application/json' | |
| }, | |
| data: data | |
| } | |
| return axios(config) | |
| .then(function (response) { | |
| const returnLink = response.data | |
| const link = returnLink.short_url | |
| return link | |
| }) | |
| .catch(function (error) { | |
| console.log(error) | |
| }); | |
| }; | |
| //This is the command to get the content ID and shorten the pin based on CID | |
| const getShortLink = (CID) => { | |
| const url = `https://api.pinata.cloud/data/pinList?status=pinned&hashContains=${CID}` | |
| const config = { | |
| method: 'get', | |
| url: url, | |
| headers: { | |
| 'Authorization': JWT, | |
| 'Content-Type': 'application/json' | |
| }, | |
| } | |
| return axios(config) | |
| .then(async function (response) { | |
| const responseData = response.data.rows | |
| responseData.forEach(row => { | |
| pinIds.push(row.id) | |
| }) | |
| for(const id of pinIds){ | |
| const link = await shortenPin(id) | |
| console.log(link) | |
| return link | |
| } | |
| }) | |
| .catch(function (error){ | |
| console.log(error) | |
| }) | |
| } | |
| //Call the function using the CID in the param | |
| getShortLink("QmSomeCIDExample") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment