The official MongoDB Stitch Browser SDK for JavaScript/TypeScript.
Run the following in the root directory of your NPM project.
This will start you off with the core SDK functionality as well as the remote MongoDB service.
See Customized Dependencies (Advanced) below for customizing dependencies.
You can also include the SDK directly in your HTML code using script tags. For core SDK functionality and the remote MongoDB service, use the following:
See Customized Dependencies (Advanced) below for customizing dependencies.
First, you need to create the server-side Stitch app, and (for the purpose of this quick start) enable anonymous authentication:
For detailed instructions, see Create a Stitch App.
Next, you create the source for your client app.
npm
installed. See npmjs.com."scripts"
field of the package.json
file that was generated by npm init
:src/index.js
and add the following code, replacing <your-client-app-id>
with the id you retrieved when setting up the application in MongoDB Stitch:dist/index.html
and add the following code:Finally, you can build and run the app:
dist/index.html
in your web browser. If everything was configured correctly, you should see a message in the browser window that you are logged in as an anonymous user.See the Getting Started guide on webpack
's website for more information on how to use webpack to bundle your JavaScript or TypeScript code that uses the Stitch SDK.
Additionally, the JavaScript code above utilizes ES6 features. If you'd like your code to run in older browsers, you'll need to use a transpiler like Babel as part of your bundling process. See babel-loader.
When your app or webpage is initialized, use Stitch.initializeDefaultAppClient to initialize the Stitch SDK. Replace <your-client-app-id>
with your Stitch application's client app ID:
Your client app ID can be found in the Stitch UI.
When running this code, you should see the following in your browser's debug console:
See StitchAuth for more information.
One of Stitch's powerful features is serverless Functions. Once logged in, the Stitch client can execute remote Stitch Functions using the StitchAppClient.callFunction method:
Assuming you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:
The echoArg
Function in Stitch would look something like:
As a convenience, the SDK includes the bson library. You can import it as you would import other classes and values from the SDK.
Here is an example of importing BSON to generate a BSON ObjectID
using ES6:
And here is an example of importing BSON to generate an ObjectId
using an HTML <script>
tag import:
For customized dependencies in NPM use the following:
For customized dependencies with HTML script tags use the following:
In the case that you don't want a single default initialized StitchAppClient
, you can use the following with as many client app IDs as you'd like to initialize clients for multiple app IDs:
You can use the client returned there or anywhere else in your app by using the following:
Generated using TypeDoc
A discriminated union type representing an auth event that might occur and should be sent to any underlying listeners.