Files
2023-08-24 20:57:44 +00:00

223 lines
18 KiB
HTML

<!doctype html>
<html class="default no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>@ngrok/ngrok-api</title>
<meta name="description" content="Documentation for @ngrok/ngrok-api">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/main.css">
<script async src="assets/js/search.js" id="search-script"></script>
</head>
<body>
<header>
<div class="tsd-page-toolbar">
<div class="container">
<div class="table-wrap">
<div class="table-cell" id="tsd-search" data-index="assets/js/search.json" data-base=".">
<div class="field">
<label for="tsd-search-field" class="tsd-widget search no-caption">Search</label>
<input id="tsd-search-field" type="text" />
</div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li>
</ul>
<a href="index.html" class="title">@ngrok/ngrok-api</a>
</div>
<div class="table-cell" id="tsd-widgets">
<div id="tsd-filter">
<a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a>
<div class="tsd-filter-group">
<div class="tsd-select" id="tsd-filter-visibility">
<span class="tsd-select-label">All</span>
<ul class="tsd-select-list">
<li data-value="public">Public</li>
<li data-value="protected">Public/Protected</li>
<li data-value="private" class="selected">All</li>
</ul>
</div>
<input type="checkbox" id="tsd-filter-inherited" checked />
<label class="tsd-widget" for="tsd-filter-inherited">Inherited</label>
<input type="checkbox" id="tsd-filter-externals" checked />
<label class="tsd-widget" for="tsd-filter-externals">Externals</label>
</div>
</div>
<a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a>
</div>
</div>
</div>
</div>
<div class="tsd-page-title">
<div class="container">
<h1>@ngrok/ngrok-api</h1>
</div>
</div>
</header>
<div class="container container-main">
<div class="row">
<div class="col-8 col-content">
<div class="tsd-panel tsd-typography">
<!-- Code generated for API Clients. DO NOT EDIT. -->
<a href="#unstable" id="unstable" style="color: inherit; text-decoration: none;">
<h1>Unstable</h1>
</a>
<p>This library is currently unstable. We know of rough edges
and are working to bring it to parity with our other API client
libraries. Please feel free to try it out and let us know if you find
it useful!</p>
<a href="#ngrok-api-client-library-for-javascript-and-typescript" id="ngrok-api-client-library-for-javascript-and-typescript" style="color: inherit; text-decoration: none;">
<h1>ngrok API client library for JavaScript and TypeScript</h1>
</a>
<p>This library wraps the <a href="https://ngrok.com/docs/api">ngrok HTTP API</a> to make it
easier to consume in JavaScript or TypeScript.</p>
<a href="#installation" id="installation" style="color: inherit; text-decoration: none;">
<h2>Installation</h2>
</a>
<p>The published library is available on
<a href="https://www.npmjs.com/package/@ngrok/ngrok-api">npm</a>.</p>
<pre><code class="language-shell"><span style="color: #000000">npm install @ngrok/ngrok-api</span>
</code></pre>
<a href="#support" id="support" style="color: inherit; text-decoration: none;">
<h2>Support</h2>
</a>
<p>The best place to get support using this library is through the <a href="https://ngrok.com/slack">ngrok Slack Community</a>. If you find any bugs, please contribute by opening a <a href="https://github.com/ngrok/ngrok-api-typescript/issues/new/choose">new GitHub issue</a>.</p>
<a href="#documentation" id="documentation" style="color: inherit; text-decoration: none;">
<h2>Documentation</h2>
</a>
<p>A quickstart guide and a full API reference are included in the <a href="https://typescript-api.docs.ngrok.com">ngrok
TypeScript API documentation</a></p>
<a href="#quickstart" id="quickstart" style="color: inherit; text-decoration: none;">
<h3>Quickstart</h3>
</a>
<p>After you&#39;ve installed the package, you&#39;ll need an API Key. Create one on the
<a href="https://dashboard.ngrok.com/api/keys">API Keys page of your ngrok dashboard</a>.</p>
<p>In your application&#39;s code, initialize an <code>Ngrok</code> client object with an API
key. API resources can be accessed as properties of the <code>Ngrok</code> object.</p>
<pre><code class="language-typescript"><span style="color: #AF00DB">import</span><span style="color: #000000"> { </span><span style="color: #001080">Ngrok</span><span style="color: #000000"> } </span><span style="color: #AF00DB">from</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;@ngrok/ngrok-api&#039;</span><span style="color: #000000">;</span>
<span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">ngrok</span><span style="color: #000000"> = </span><span style="color: #0000FF">new</span><span style="color: #000000"> </span><span style="color: #795E26">Ngrok</span><span style="color: #000000">({</span>
<span style="color: #000000"> </span><span style="color: #001080">apiToken:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;&lt;API KEY&gt;&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000">});</span>
<span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">domain</span><span style="color: #000000"> = </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">domains</span><span style="color: #000000">.</span><span style="color: #795E26">create</span><span style="color: #000000">({</span>
<span style="color: #000000"> </span><span style="color: #001080">name:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;your-name.ngrok.io&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000">});</span>
<span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #001080">domain</span><span style="color: #000000">);</span>
</code></pre>
<a href="#automatic-paging" id="automatic-paging" style="color: inherit; text-decoration: none;">
<h3>Automatic Paging</h3>
</a>
<p>The ngrok API pages all list resources but this library abstracts that
implementation detail away from you. <code>list()</code> methods will return collections
that can be iterated over and the implementation will fetch the pages from the
API for you behind the scenes.</p>
<pre><code class="language-typescript"><span style="color: #AF00DB">import</span><span style="color: #000000"> { </span><span style="color: #001080">Ngrok</span><span style="color: #000000"> } </span><span style="color: #AF00DB">from</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;@ngrok/ngrok-api&#039;</span><span style="color: #000000">;</span>
<span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">ngrok</span><span style="color: #000000"> = </span><span style="color: #0000FF">new</span><span style="color: #000000"> </span><span style="color: #795E26">Ngrok</span><span style="color: #000000">({</span>
<span style="color: #000000"> </span><span style="color: #001080">apiToken:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;&lt;API KEY&gt;&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000">});</span>
<span style="color: #000000">(</span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">tunnels</span><span style="color: #000000">.</span><span style="color: #795E26">list</span><span style="color: #000000">()).</span><span style="color: #795E26">forEach</span><span style="color: #000000">(</span><span style="color: #001080">t</span><span style="color: #000000"> </span><span style="color: #0000FF">=&gt;</span><span style="color: #000000"> </span><span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #001080">t</span><span style="color: #000000">));</span>
</code></pre>
<a href="#async-programming" id="async-programming" style="color: inherit; text-decoration: none;">
<h3>Async Programming</h3>
</a>
<p>All API methods return a <code>Promise</code> and are suitable for use in asynchronous
programming. You can use callback chaining with <code>.then()</code> and <code>.catch()</code> syntax
or the <code>await</code> keyword to wait for completion of an API call.</p>
<pre><code class="language-typescript"><span style="color: #008000">// await style</span>
<span style="color: #0000FF">const</span><span style="color: #000000"> </span><span style="color: #0070C1">cred</span><span style="color: #000000"> = </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">credentials</span><span style="color: #000000">.</span><span style="color: #795E26">create</span><span style="color: #000000">({ </span><span style="color: #001080">description:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;example&#039;</span><span style="color: #000000"> });</span>
<span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #001080">cred</span><span style="color: #000000">);</span>
<span style="color: #008000">// callback chaining</span>
<span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">credentials</span><span style="color: #000000">.</span><span style="color: #795E26">create</span><span style="color: #000000">({ </span><span style="color: #001080">description:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;example&#039;</span><span style="color: #000000"> }).</span><span style="color: #795E26">then</span><span style="color: #000000">(</span><span style="color: #001080">cred</span><span style="color: #000000"> </span><span style="color: #0000FF">=&gt;</span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #001080">cred</span><span style="color: #000000">);</span>
<span style="color: #000000">});</span>
</code></pre>
<a href="#error-handling" id="error-handling" style="color: inherit; text-decoration: none;">
<h3>Error Handling</h3>
</a>
<p>The ngrok API returns detailed information when an API call fails. If an error
is encountered, API methods throw the rich <code>Error</code> type on resolution of a
returned <code>Promise</code>. This allows your code to gracefully handle different error
conditions.</p>
<p>The <code>Error</code> type includes a <code>statusCode</code> property which can be used to
distinguish not found errors when a resource does not exist:</p>
<pre><code class="language-typescript"><span style="color: #AF00DB">import</span><span style="color: #000000"> { </span><span style="color: #001080">Error</span><span style="color: #000000"> } </span><span style="color: #AF00DB">from</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;@ngrok/ngrok-api&#039;</span><span style="color: #000000">;</span>
<span style="color: #AF00DB">try</span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">ipPolicies</span><span style="color: #000000">.</span><span style="color: #795E26">update</span><span style="color: #000000">({</span>
<span style="color: #000000"> </span><span style="color: #001080">id:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;someInvalidId&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000"> </span><span style="color: #001080">description:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;updated description&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000"> });</span>
<span style="color: #000000">} </span><span style="color: #AF00DB">catch</span><span style="color: #000000"> (</span><span style="color: #001080">err</span><span style="color: #000000">: </span><span style="color: #267F99">Error</span><span style="color: #000000">) {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">if</span><span style="color: #000000"> (</span><span style="color: #001080">err</span><span style="color: #000000">.</span><span style="color: #001080">statusCode</span><span style="color: #000000"> == </span><span style="color: #098658">404</span><span style="color: #000000">) {</span>
<span style="color: #000000"> </span><span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #A31515">&#039;no ip policy with that id to update&#039;</span><span style="color: #000000">);</span>
<span style="color: #000000"> }</span>
<span style="color: #000000">}</span>
</code></pre>
<p>Every error returned by the ngrok API includes a <a href="https://ngrok.com/docs/errors">unique, documented error
code</a> that you can use to distinguish unique
error conditions. Use the <code>errorCode</code> property in your application code to
handle handle different error conditions.</p>
<pre><code class="language-typescript"><span style="color: #AF00DB">import</span><span style="color: #000000"> { </span><span style="color: #001080">Error</span><span style="color: #000000"> } </span><span style="color: #AF00DB">from</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;@ngrok/ngrok-api&#039;</span><span style="color: #000000">;</span>
<span style="color: #AF00DB">try</span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">await</span><span style="color: #000000"> </span><span style="color: #001080">ngrok</span><span style="color: #000000">.</span><span style="color: #001080">ipPolicies</span><span style="color: #000000">.</span><span style="color: #795E26">create</span><span style="color: #000000">({</span>
<span style="color: #000000"> </span><span style="color: #001080">action:</span><span style="color: #000000"> </span><span style="color: #A31515">&#039;something invalid&#039;</span><span style="color: #000000">,</span>
<span style="color: #000000"> });</span>
<span style="color: #000000">} </span><span style="color: #AF00DB">catch</span><span style="color: #000000"> (</span><span style="color: #001080">err</span><span style="color: #000000">: </span><span style="color: #267F99">Error</span><span style="color: #000000">) {</span>
<span style="color: #000000"> </span><span style="color: #AF00DB">if</span><span style="color: #000000"> (</span><span style="color: #001080">err</span><span style="color: #000000">.</span><span style="color: #001080">errorCode</span><span style="color: #000000"> == </span><span style="color: #A31515">&#039;ERR_NGROK_1410&#039;</span><span style="color: #000000">) {</span>
<span style="color: #000000"> </span><span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #A31515">&#039;not a valid ip policy action&#039;</span><span style="color: #000000">);</span>
<span style="color: #000000"> } </span><span style="color: #AF00DB">else</span><span style="color: #000000"> {</span>
<span style="color: #000000"> </span><span style="color: #001080">console</span><span style="color: #000000">.</span><span style="color: #795E26">log</span><span style="color: #000000">(</span><span style="color: #A31515">&#039;some other error&#039;</span><span style="color: #000000">, </span><span style="color: #001080">err</span><span style="color: #000000">);</span>
<span style="color: #000000"> }</span>
<span style="color: #000000">}</span>
</code></pre>
</div>
</div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<nav class="tsd-navigation primary">
<ul>
<li class=" ">
<a href="modules.html">Modules</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/client.html">client</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/datatypes.html">datatypes</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/services.html">services</a>
</li>
</ul>
</nav>
<nav class="tsd-navigation secondary menu-sticky">
<ul class="before-current">
</ul>
</nav>
</div>
</div>
</div>
<footer class="with-border-bottom">
<div class="container">
<h2>Legend</h2>
<div class="tsd-legend-group">
<ul class="tsd-legend">
<li class="tsd-kind-interface"><span class="tsd-kind-icon">Interface</span></li>
</ul>
<ul class="tsd-legend">
<li class="tsd-kind-class"><span class="tsd-kind-icon">Class</span></li>
</ul>
</div>
</div>
</footer>
<div class="container tsd-generator">
<p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p>
</div>
<div class="overlay"></div>
<script src="assets/js/main.js"></script>
</body>
</html>