Follow this guide to use javascript client SDK in your web app or any Javascript/Node.js project.
Install via npm:
npm install space-api --saveOr import as a stand-alone library:
<script src="https://storage.googleapis.com/space-cloud/libraries/space-api.js"></script>An api instance of Space Cloud on the frontend helps you talk to space-cloud binary and perform backend operations directly from the frontend.
The API constructor takes two parameters:
TodoApp becomes todoapp.space-cloud binary. It’s http://localhost:4122 or https://localhost:4126 for HTTP and HTTPS endpoints respectively.Note: Replace
localhostwith the address of your Space Cloud if you are not running it locally.
For ES6:
import { API } from 'space-api';
const api = new API('todo_app', 'http://localhost:4122');
For ES5/CommonJS:
const { API } = require('space-api');
const api = new API('todo_app', 'http://localhost:4122');
For stand-alone:
var api = new Space.API("todo_app", "http://localhost:4122");
The API instance created above helps you to use file storage and services modules directly. However, to use database and realTime modules, you also have to create a db instance.
Note: You can use multiple
dbinstances in the same project as space cloud supports multiple databases.
This is how you create an instance of db in your project:
const db = api.DB(<db-alias-name>);
Let’s say you have provided the alias name for your database as mydb while adding your database in Mission Control, then your db initialization code will be:
const db = api.DB("mydb");
The alias name identifies a database in your project uniquely. You can find out the alias name for your database from the database selector of the topbar in the Database section of Mission Control.
Great! Since you have initialized the api and db instance, you can start building apps with space-cloud.
Feel free to check out various capabalities of space-cloud: