24 Oct 2017

RunKit NodeJS playground Forge Sample

Default blog image

A couple of weeks ago I started to investigate if there was a web solution similar to Codepen but for the Server side.
I came across a couple of different solutions, but the one that worked for me was RunKit Notebook

Using RunKit, with one click, you'll have a sandboxed JavaScript environment where you can instantly switch node versions, use every NPM module without having to wait to install it, and even visualize your results. No more configuration, just straight to coding. Create an API without worrying about servers or configuration. Just export a endpoint function and your notebook automatically becomes an HTTPS endpoint, accessible from any app. Great for prototyping iOS and Android backend, or creating micro services.

When I found that you could do this with the endpoints, I ended up using it to create an API endpoint to be able to later fetch a 2-legged token. What I also liked was how easy it was to embed a runkit notebook on this blog post. Replace the client ID and client Secret from your Forge App Keys and launch the endpoint.

var express = require("@runkit/runkit/express-endpoint/1.0.0");
var router = express(module.exports);
var cors = require('cors')
var forgeSDK = require('forge-apis');

// The best recommendation is to clone this notebook and set ENV Vars for your own account

var forge_client_id = process.env.FORGE_CLIENT_ID || "Your Forge Client ID goes here...";

var forge_client_secret = process.env.FORGE_CLIENT_SECRET || "Your Forge Client Secret goes here...";

var scope = "viewables:read"; // Scopes for your Token

router.use(cors())

router.get("/", (req, res) => {
var request = new forgeSDK.AuthClientTwoLegged(forge_client_id, forge_client_secret, [scope])
request.authenticate().then(function (credentials) {
res.send({ access_token: credentials.access_token, expires_in: credentials.expires_in });
})
})

 
In my next post, I will show you how I use this endpoint on a client side app being used with Codepen. 

Related Article