Search code examples
javascripttokennode-modulesgithub-pagesgithub-api

GitHub API create file not creating


I was trying to create a website where I could put in some data, and it would send GitHub API a POST request to create a file with the specified data. I am using GitHub Pages to host.

I created a test script that should create a file once the page loads, and it worked originally, but as I came back to test it again 2 hours later, it didn't create a file. What am I doing wrong? Note: I'm using personal tokens for this, is that an issue?

Here's my code:

index.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Dev portal</title>
  <link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
  Hello world
  <script type="module" src="script.js"></script>
</body>

</html>

script.js:

import { Octokit } from "https://cdn.skypack.dev/@octokit/rest";
  const octokit = new Octokit({
  auth: 'iputmytokenhere'
})

await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
  owner: 'Skoolgq',
  repo: 'skoolgq.github.io',
  path: 'test2.txt',
  message: 'Test commits for JSON API',
  committer: {
    name: 'Skool Developers',
    email: '[email protected]'
  },
  content: 'bXkgbmV3IGZpbGUgY29udGVudHM='
})

Solution

  • Turns out I was using an existing filename. Silly me!