Use any API in seconds with auth managed by Pipedream
Tags: node, javascript, github, tutorial
Published: 2020-01-25 03:25:52

<p><a href="https://pipedream.com">Pipedream</a> makes it easy to write Node.js and use any API in seconds! We securely manage auth for 100+ apps (with <a href="https://github.com/PipedreamHQ/roadmap/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+is%3Aclosed+">more added daily</a>) – just connect your app accounts and use the <code>auths</code> object in code to reference tokens and keys.</p><h2 id="github-http-api">Github HTTP API</h2><p>For example, to use Github's HTTP API,  just connect your account using a browser-based oauth flow and and pass <code>auths.github.access_token</code> as the value of the <code>Bearer</code> token (check out the <code>octokit</code> section below for an example of how to use <code>auths</code> with an npm package).</p><pre><code class="language-javascript">const axios = require('axios')

const response = await axios({
  url: `https://api.github.com/user`,
  headers: {
    Authorization: `Bearer ${auths.github.oauth_access_token}`,
  }, 
})

return response.data</code></pre><p>Here's a GIF that demonstrates how to use the Github API in 30-seconds:</p><figure class="kg-card kg-image-card"><img src="https://blog.pipedream.com/content/images/2020/01/githubdemo.gif" class="kg-image"></figure><p>In ~30-seconds, I add a code step with Github auth, paste in the code above, connect my account, query the API and inspect the results. <a href="https://pipedream.com/@pravin/github-demo-p_brCnan">Copy this template</a> to try it yourself .</p><h2 id="github-via-octokit">Github via Octokit </h2><p>You're not limited to using Pipedream managed auth with HTTP APIs – it works with npm packages too! Here's an example of the same API request as above, but using Github's <code>octokit</code> package:</p><pre><code class="language-javascript">const Octokit = require('@octokit/rest')

const octokit = new Octokit({
  auth: auths.github.oauth_access_token
})

return (await octokit.users.getAuthenticated()).data</code></pre><p>Pipedream transparently installs any npm packages you require in your code when you deploy. To try it yourself, <a href="https://pipedream.com/@pravin/github-demo-p_brCnan">copy this template</a>, enable <code>steps.octokit</code>, connect your account and run the code.</p><figure class="kg-card kg-image-card"><img src="https://blog.pipedream.com/content/images/2020/01/octokitdemo.gif" class="kg-image"></figure><h2 id="scaffolding-api-auth-code">Scaffolding API + Auth Code</h2><p>Pipedream also supports actions, which are re-usable code steps. Actions provides scaffolding for popular APIs and functions. To scaffold a generic API for an app, just click the plus (+) button <strong>, </strong>select the app and choose <strong>Run Node.js with [App Name]</strong>:</p><figure class="kg-card kg-image-card"><img src="https://blog.pipedream.com/content/images/2020/01/basicscaffolding-1.gif" class="kg-image"></figure><p>To scaffold a specific API for an app, just click the plus (+) button , select the app and choose the action that you want to scaffold. Try editing the code and confirm that you want to customize it:</p><figure class="kg-card kg-image-card"><img src="https://blog.pipedream.com/content/images/2020/01/actionscaffold-1.gif" class="kg-image"></figure><h2 id="triggering-your-code">Triggering Your Code</h2><p>Trigger your code:</p><ul><li>Manually</li><li>On an interval or cron schedule</li><li>On HTTP request (Pipedream will generate a unique URL — any requests to this URL will trigger your code)</li><li>On an email (Pipedream will generate a unique email address — any emails sent to this address will trigger your code)</li><li>Via the Pipedream Node.js or Ruby SDKs</li></ul><h2 id="getting-started">Getting Started</h2><p>To get started, just sign in with your Google or Github account at <a href="https://pipedream.com">https://pipedream.com</a>. Check out the project templates at <a href="https://pipedream.com/explore">https://pipedream.com/explore</a>, and join our public Slack at <a href="https://pipedream.com/community">https://pipedream.com/community</a>!</p>