Home    |    GitHub Page    |    API    |    FAQ

Can’t set new post ID through the data API?

So, I’m trying to export my posts from AniList as new notes in Joplin (that do not previously exist. I made sure to create a unique ID through a function based on the post ID on that site. That way I can later update the notes whenever I get a «this not already exist» response. However, for some reason it doesn’t create the notes with the ID I set it as?

Here’s my conversion to a Joplin-compatible ID.

// Converting PostID to to 32 characters
        postID = "414c5f414354" + postID.toString(16).padStart(32 - "414c5f414354".length, "0"); // 16 is optional. Changes it to hex

An example post ID from AniList is: 138726754

Here’s the code I parse into JSON to send to Joplin:

// Sending the data to Joplin
        fetch("http://localhost:41184/notes?" + joplinToken, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            "title": postTitle,
            "parent_id": parentID,
            "id": postID,
            "body": postWithCommentsAndNames,
            "user_created_time": postCreated
          })
        }).then(res => {
          console.log("Request complete! response:", res);
        });

A link to the source code on GitHub. All the code is in the file AniListFetch.js

Keep in mind that this isn’t the latest version. It still works, but it doesn’t go through all pages, only the first, and it doesn’t fall back on trying to update if it fails to create.

I’m not at my PC atm, and won’t be for the next 14 hours. But if I have to provide additional information, I’m do what I can. Joplin on mobile (iOS) doesn’t seem to let me check note IDs tho.

I see nothing obviously wrong in the code above, but make sure you handle errors (any response with HTTP code greater than 400) so that you know if there's a problem.

Also if you want to update a note, use PUT, not POST. I'm not even sure what will happen if you post a note with an ID that already exists, I guess it will just fail at SQL level somewhere.

1 Like

Seems it was an issue with syncing or something. After importing (probably right after having deleted them, with the sync duration set to disabled, so it shouldn't have been syncing?) , it would go and fetch the notes online and then it somehow replaced the IDs when it did?

Well, anyway, it seems that if I do a sync right after I delete all of them, it works out fine. I've been doing that a lot to test if things are working properly.

Also, the code I showed a sample of was a bit old and based on what I could get at that time. Here's some more recent code which uses a PUT method if the POST fails (if the note ID already exist, it returns a 500 (Internal Server Error).

// Converting PostID to to 32 characters
        postID = "414c5f414354" + postID.toString(16).padStart(32 - "414c5f414354".length, "0"); // 16 is optional. Changes it to hex

        let toJoplin = {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            "id": postID,
            "title": postTitle,
            "parent_id": parentID,
            "body": postWithCommentsAndNames,
            "created_time": postCreated,
            "user_created_time": postCreated
          })
        };

        //Sending the data to Joplin
        fetch("http://localhost:41184/notes?" + joplinToken, toJoplin)
        .then(res => {
          console.log(res)


          if (res.ok) {
            console.log("Request complete! response:", res);
          } else {
              fetch("http://localhost:41184/notes/" + postID + "?" + joplinToken, {
              method: 'PUT',
              headers: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify({
                "id": postID,
                "body": postWithCommentsAndNames
              })
            }).then(res => {
                console.log("Your note was updated! response:", res);
            })
          }
        });

Link to the full code. Keep in mind that I've just copy-pasted the bottleneck ratelimiting code to my document because I was facing problems with CORS and stuff and was too lazy to research and fix that, so I just put it in the same code. That means my code doesn't start until line 5067.

Now all I have to do is some more clean-up and make some simple HTML page so even dumb-dumbs could use this tool if they wanted to (by just plotting in the necessary info)

Btw, quick extra question. The authorisation token for Joplin, is it an issue if it appears in my GitHub? I accidentally committed once with it in and decided to just do a refresh and delete my commit history. Not sure if that was necessary considering it's doing localhost?

Yes and no. Usually it shouldn't matter, since the clipper is rstrsiced to localhost. However, web apps like this one also run locally.

Unfortunately there's no button to renew the token. However, you can do it manually:

  • quit Joplin
  • open database.sqlite in your profile dir
  • delete record key=api.token from table settings (delete from settings where key='api.token')
  • start Joplin
2 Likes

Thanks a bunch! I’ll do this tomorrow just in case :slight_smile: