The official MongoDB Stitch Server 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.
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.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:node src/index.js
.When your app has started, 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.
We enabled anonymous authentication in the steps above, so let's log in with it! Add the following anywhere in your code:
When running this code, you should see the following in your standard out:
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
:
For customized dependencies, 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:
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:
You can use the client returned there or anywhere else in your app by using the following:
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:
Generated using TypeDoc
A discriminated union type representing an auth event that might occur and should be sent to any underlying listeners.