- Create env file in backend
- Generate your Canvas access token
- Start the server
- Create an API call to Canvas from within server.js
- Next step
- Create a
.env
file inside thebackend
folder. It's easiest to create this file using a code editor like VSCode. - Copy the snippet below into
.env
.CANVAS_API_TOKEN= CANVAS_API_DOMAIN=https://ubc.instructure.com/api/v1
- In the next step, we'll create a token and paste it in the
CANVAS_API_TOKEN=
field.
- Log into Canvas at canvas.ubc.ca. Click Account in the left menu, and then click Settings.
- Scroll to Approved Integration and click + New Access Token.
- Fill in the Purpose field. For added security, set an expiry date for your token. This way, if you accidentally share your token or your token is stolen, at the very least it won’t be valid forever.
- Click Generate Token. Now copy your freshly generated token.
- In the
.env
,CANVAS_API_TOKEN={Paste your token here}
- Save
.env
.
Never share your token with anyone. If you think your token may have been exposed (for example, by accidentally posting it to GitHub), delete your token from Canvas right away. Instructions for creating and deleting access tokens as a student are available on the Canvas Guides.
- Type
npm start
into terminal inbackend
directory. If your server is already running, kill it first:control + c
, then start the server. If you see any strange error messages, you might have forgotten to install dependencies usingnpm install
. - Navigate to http://localhost:4001/.
You should see a Hello World!
displayed.
Not sure what an API (application programming interface) is? Here's a guide.
To ensure that you are able to make API calls to Canvas, let's create a call and print the result.
Navigate to server.js
in backend
folder, and you'll see the following line:
const canvasAPI = require('node-canvas-api')
This line imports a Canvas API object that comes with a bunch of useful functions. The one we want to use is the simplest: getSelf
. This returns information about the person who makes the request.
So add the following line below // Make API call to Canvas API here
:
canvasAPI.getSelf()
.then(self => console.log(self))
Once you save the file, the server will automatically restart.
You should receive response back that looks something like this printed in the terminal:
{
id: 50,
name: 'Justin Lee',
created_at: '2017-07-12T10:38:49-07:00',
sortable_name: 'Lee, Justin',
short_name: 'Justin Lee',
avatar_url: 'https://ubc.instructure.com/images/messages/avatar-50.png',
locale: null,
effective_locale: 'en-CA',
permissions: { can_update_name: false, can_update_avatar: true }
}
If you encounter an error, please ask for help!
Now you're ready to go to Step 2: Create API endpoints in backend.