/    Sign up×
Bounties /Pin to ProfileBookmark

GitHub API create file not creating when using input box

+ 2,500
Copy linkTweet thisAlerts:
Jan 21.2023

Why isn’t my file created when I use an input box for the file name?

Hi, I’m trying to create a webpage where I can enter a name into an input box, and when I click a button, it will take the value from the input box and use that as the file name. However, my code doesn’t seem to work once I put a variable into the “path” attribute.

I tested with some simple code to test whether it had something to do with the input box, but that seemed to work: 

But once I added the API code, it didn’t create a file in the repo:

Any help is appreciated, thanks!

to post a answer

1 Replies

Copy linkTweet thisAlerts:
@jordan-thirkleJan 26.2023 — Problem
It looks like the problem with your code is that you're trying to use the Octokit library for interacting with the GitHub API, but you're not actually importing it correctly.

Instead of using import { Octokit } from "https://cdn.skypack.dev/@octokit/rest";,
You should use a package manager such as NPM to install the library and import it into your code.

1. Install Octokit by running npm install @octokit/rest in your terminal.
2. Import Octokit in your javascript file like this:
const { Octokit } = require("@octokit/rest");
3. Use the Octokit instance to make API calls:
const octokit = new Octokit({
auth: 'iputmypersonaltokenhere'

const filename = document.getElementById("filename").value;

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

Note:The above code is using the request method, which is asynchronous. It should be used with await keyword or within an async function.

Another Note: You should use the token of a GitHub account with the correct permissions to access the repository where you want to create a file.

Hope this helps solve your issue :).
@1authorThanks a lot for your help!Jan 27.2023


Help @1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 2.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,

tipper: @neststayhome,
tipped: article
amount: 1000 SATS,