This project was created using the following process:
-
Fork/clone/download this project into your workspace.
-
Install dependencies
npm install
-
Run the server locally:
npm run dev
Note: A Personal Access Token is needed to display your own Gists. Otherwise, public gists will be displayed.
- GitHub Personal Access Token created.
-
Create a new Nuxt 3 Minimal Starter project in your workspace:
npx nuxi init nuxt-github-api
You can replace
nuxt-github-api
with a directory name of your choice. -
Install the Octokit JS Library in this new project:
npm install @octokit/core
-
Create a
.env
file in the project root containing your GitHub Personal Access Token:TOKEN="personal-access-token123"
-
Create a
/server/api/github.js
file in the project root. -
In
github.js
import the Octokit package and create a newoctokit
object using yourTOKEN
:import { Octokit } from "@octokit/core"; const octokit = new Octokit({ auth: process.env.TOKEN });
-
In the same
github.js
file, now export a default function that will handle API requests and return data from the GitHub API:export default async (req, res) => { const response = await octokit.request('GET /gists?per_page=100', { org: "octokit", type: "private", }); return response.data; }
-
The GitHub API data should be available at
/api/github
!
-
Create a
/pages/gists.vue
page in the project root. -
Inside this
gists.vue
page, call the/api/github
API you just created using the Composition API:<script setup> const { data: gists } = await useAsyncData("gists", () => $fetch("/api/github"), ); </script>
-
gists
now contains an array that can be displayed ingists.vue
using a loop:<template> <div> <h2>Gists</h2> <ul> <li v-for="gist in gists" :key="gist.id"> {{ gist.description }} </li> </ul> </div> </template>