Nettu-API

Integration with existing users

Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

integration diagram

1. Get Nettu user token

In the first step you will need to query your server for a Nettu user token in order to use the SDK.

2. Use the secret api key to create an access token for the enduser

In this step you will have to authenticate your user and find which nettu user id it belongs to and thereafter request a token for that user. The authentication logic is probably something you already have in place and its implementation is not of concern to this integration process. With every user in your application that wish to take advantage of the Nettu API it should be created a corresponding nettu user so that your application can link them. If the user that is requesting a token does not yet have a nettu user id stored with it, you can easily create one as follows:

tutorial.js
const { NettuClient } = require('@nettu/sdk-js');
// Create a new instance of the NettuClient class with the api_key / token read from your environment variable
const client = NettuClient.create(process.env.NETTU_API_KEY);
(async () => {
try {
// Use the `users.create` method to create a user associated with your Nettu app
const { userId } = await client.users.create();
console.log(`User created with the following userId: ${userId}`);
// Store this userId together with your user object
} catch (error) {
console.log(error);
}
})();

The returned userId is a nettu user id that you can store with your user in your database so that you can reference it in future requests. All bookings, purchases etc will be stored with this userId, so it is important to save it. Once you have a nettu user id associated with your user you can use the nettu sdk to issue user tokens with a call to the users.createToken method as shown below:

tutorial.js
const { NettuClient } = require('@nettu/sdk-js');
// Create a new instance of the NettuClient class with the api_key / token read from your environment variable
const client = NettuClient.create(process.env.NETTU_API_KEY);
const NETTU_USER_ID = "REPLACE_ME"; // replace this with the userid of the user you created in the previous step
(async () => {
try {
// Use the `users.createToken` to create a token for your user
const { accessToken } = await client.users.createToken(NETTU_USER_ID);
// The received accessToken can then be passed back from your backend
// to the frontend where your user can use it
console.log(`Created an access token for user: ${NETTU_USER_ID} with value: ${accessToken}`);
} catch (error) {
console.log(error);
}
})();

3. Return created token back to the frontend

Back in your frontend you can create a NettuClient for your enduser with the newly created access token.

tutorial.js
const { NettuClient } = require('@nettu/sdk-js');
// Replace this function with a call against your server which then returns the user token you
// got from the Nettu API.
async getAccessTokenFromYourServer = () => {
return "USER_TOKEN";
}
(async () => {
const accessToken = await getAccessTokenFromYourServer();
// Create a new instance of the NettuClient class with the token
const client = NettuClient.create(accessToken);
// Some examples:
client.bookings.list(); // List upcoming bookings for user
client.calendars.connectGoogle(); // Let user connect his / her google calendar to your Nettu app
client.availibility.employees({ tag: 'math' }); // Get availibility calendar of all instructors with the tag: math
// + many more for you to explore! 🚀
})();

4. Frontend Nettu SDK is now ready! 🚀

Your frontend Nettu SDK now has complete access to all SDK methods which your user is authorized to call, and can call them directly without communicating with your server! It can list bookings, manipulate calendars, check available employees, connect to google calendar and so much more for you to explore.

Edit this page on GitHub