The official MongoDB Stitch Browser SDK for JavaScript/TypeScript.
Run the following in the root directory of your NPM project.
npm install mongodb-stitch-browser-sdk
This will start you off with the core SDK functionality as well as the remote MongoDB service.
For customized dependencies use the following:
npm install mongodb-stitch-browser-core
npm install mongodb-stitch-browser-services-aws-s3
npm install mongodb-stitch-browser-services-aws-ses
npm install mongodb-stitch-browser-services-http
npm install mongodb-stitch-browser-services-mongodb-remote
npm install mongodb-stitch-browser-services-twilio
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:
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch.js"></script>
For customized dependencies use the following:
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-core.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-services-aws-s3.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-services-aws-ses.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-services-http.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-services-mongodb-remote.js"></script>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.7/stitch-services-twilio.js"></script>
npm
installed. See npmjs.com.npm init
.npm install mongodb-stitch-browser-sdk
.webpack.js
by running npm install --save-dev webpack webpack-cli
."scripts"
field of your package.json
:"scripts": {
"pack": "webpack"
}
mkdir src dist
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:import { Stitch, AnonymousCredential } from 'mongodb-stitch-browser-sdk'
function initializeAndLogin() {
const client = Stitch.initializeDefaultAppClient('<your-client-app-id>');
client.auth.loginWithCredential(new AnonymousCredential()).then(user => {
document.getElementById('auth-status').innerHTML =
`Logged in as anonymous user with id ${user.id}`;
});
}
window.onload = initializeAndLogin;
dist/index.html
and add the following code:<!doctype html>
<html>
<head>
<title>MongoDB Stitch Sample</title>
</head>
<body>
<script src="main.js"></script>
<div id="auth-status">Logged Out</div>
</body>
</html>
npm run pack
.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.
<your-client-app-id>
with your Stitch application's client app ID:import { Stitch, AnonymousCredential } from 'mongodb-stitch-browser-sdk'
Stitch.initializeDefaultAppClient('<your-client-app-id>');
Stitch.defaultAppClient
const stitchClient = Stitch.defaultAppClient;
const client = Stitch.defaultAppClient;
console.log("logging in anonymously");
client.auth.loginWithCredential(new AnonymousCredential()).then(user => {
console.log(`logged in anonymously as user ${user.id}`)
});
logging in anonymously
logged in anonymously as user 58c5d6ebb9ede022a3d75050
StitchAppClient
's callFunction()
methodclient.callFunction("echoArg", ["Hello world!"]).then(echoedResult => {
console.log(`Echoed result: ${echoedResult}`);
})
Echoed result: Hello world!
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:
const client = Stitch.initializeAppClient("<your-client-app-id>");
You can use the client returned there or anywhere else in your app by using the following:
const client = Stitch.getAppClient("<your-client-app-id>");
Generated using TypeDoc