The official MongoDB Stitch Server SDK for JavaScript/TypeScript.
Run the following in the root directory of your NPM project.
npm install mongodb-stitch-server-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-server-core
npm install mongodb-stitch-server-services-aws-s3
npm install mongodb-stitch-server-services-aws-ses
npm install mongodb-stitch-server-services-http
npm install mongodb-stitch-server-services-mongodb-remote
npm install mongodb-stitch-server-services-twilio
npm
installed. See npmjs.com.npm init
.npm install mongodb-stitch-server-sdk
.mkdir src
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:const {
Stitch,
AnonymousCredential,
} = require('mongodb-stitch-server-sdk');
const client = Stitch.initializeDefaultAppClient('<your-client-app-id>');
client.auth.loginWithCredential(new AnonymousCredential()).then(user => {
console.log(user);
client.close();
}).catch(err => {
console.log(err);
client.close();
})
node src/index.js
.<your-client-app-id>
with your Stitch application's client app ID:const { Stitch, AnonymousCredential } = require('mongodb-stitch-server-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>");
Clients will generally store their data in $HOME/.stitch/<your-client-app-id>
. To modify this directory, you must initialize an app client with a custom data directory set:
const { Stitch, StitchAppClientConfiguration } = require('mongodb-stitch-server-sdk');
const client = Stitch.initializeAppClient("<your-client-app-id>", new StitchAppClientConfiguration.Builder().withDataDirectory('/some/path').build());
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>");
The client maintains some background processes in the event loop that must be shutdown when the client is no longer needed. Simply call close
on the client when you are done with the client:
client.close();
Generated using TypeDoc