Search code examples
sendbird

How to create SendBird user with SendBird Platform API and Request node library


We are building a react-native chat app. We are implementing a back end authentication solution on google Firebase. The creation of a new user in Firebase Auth triggers a cloud function which should create a new SendBird user with an access token. The access token will be stored in Cloud Firestore, ready for retrieval the next time the user logs in.

We are having trouble implementing the POST request that creates the new user via the platform API. We are using the Request library for node.js. We are able to reach the API endpoint, but the following object is returned: { message: 'SendBird API Endpoint.', error: true }. There is no indication of what the error may be.

This happens when sending the request to the base url. When we send the request to /users or /v3/users, we receive a 403 error.

Any indication as to what may be causing this problem would be greatly appreciated.

Below is the cloud function index.js code

const functions = require('firebase-functions');
const admin = require("firebase-admin");
const request = require('request');

admin.initializeApp();

exports.handleNewUser = functions.auth.user().onCreate((user) => {

    var newUserRequestBody = {
    "user_id": user.email,
    "nickname": user.email,
    "profile_url": "",
    "issue_access_token": true,
    }

    request.post({
      headers: {
          'Content-Type': 'application/json, charset=utf8',
          'Api-Token': // API Token
      },
      url:     'https://api-{application_id}.sendbird.com',
      form:    newUserRequestBody
    }, function(error, response, body){
        if (!error && response.statusCode === 200) {
          const info = JSON.parse(body);
          console.log("request successful");
          console.log(response.statusCode);
          console.log(info);
        }
        else{
            console.log("request unsuccessful");
            console.log(response.statusCode);
            console.log(error);
        }
    });

    return null;
  });

Solution

  • Did you try with full path of end point to url: (including /v3/users)? Or you may need to use "baseUrl" like below? https://github.com/request/request#requestoptions-callback

    Also, you need to make sure that you correctly used {application_id} value and {API Token} value. You can double check this from your dashboard of SendBird. http://dashboard.sendbird.com > Log in with your ID > select your APP. There is a section named "App credentials" in "Overview" menu. You can double check your API-request URL and API-Token value from there.