Minimal Node Integration to Get You Started with Stream

3 min read

If you start to explore Stream’s services, you’ll need an integration layer to handle fetching user tokens from our backend without exposing your API credentials to your client implementation.

Jeroen L.
Jeroen L.
Published January 23, 2023

What this sample offers is a very basic login and sign-up endpoint that interacts with the Stream Chat and Feeds API to supply you with user access tokens. With these tokens, you can then integrate using one of our client SDKs or Rest API.

Getting Started

First, have a look at the git repository containing the sample code.

What you want is all contained in the server directory.

shell
git clone https://github.com/GetStream/stream-node-simple-integration-sample.git
cd stream-node-simple-integration-sample
ls

As you can see in the server/routes/auth.js file, this codebase is very straightforward.

javascript
const express = require("express");

const { signup, login } = require("../controllers/auth.js");

const router = express.Router();

router.post("/signup", signup);
router.post("/login", login);

module.exports = router;

Just two routes, and you can probably guess the purpose of each.

Before you can run our server, make sure to add your Stream credentials to the .env file. Copy the sample and replace the placeholders in the .env file.

I am using vi (I should say vim), but use your own editor of choice. You can get out of Vim by pressing esc and then :q! followed by <enter>.

shell
cp server/.env.sample server/.env
vi server/.env

The .env file contains placeholders:

STREAM_APP_ID = [stream-app-id]
STREAM_API_KEY = [stream-api-key]
STREAM_API_SECRET = [stream-api-secret]

Make sure to replace the placeholder including the brackets.

The end result should look somewhat like this:

Note, the credentials in the next snippet are NOT valid.

STREAM_APP_ID = 999xx99
STREAM_API_KEY = bcdefgssdad
STREAM_API_SECRET = 
dlfjslf78sdfslmdfu283rfndslf934alsjf239u84raslkdfj0239uriwesflsd

Once you configured your credentials it is time to run things.

The steps are straightforward. CD into the server directory, run yarn install or npm install and then run yarn start or npm start.

I'm going with the NPM route myself.

shell
cd server
npm install
npm start

If everything went correctly you should see this at the bottom of your terminal:

Server running on port 8080

Testing the integration with your new backend

You can use Postman or any other API testing tool for this as well. But to keep things simple, I will stick with the terminal.

Try and run a curl command that accesses the login endpoint.

shell
curl -X "POST" "http://localhost:8080/auth/login" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "username": "jeroen",
  "password": "password"
}'

You will get a message back stating the user does not exist.

json
{"message":"User with this username doesn't exist."}

Which is correct, I did not register yet.

So let's try that next.

shell
curl -X "POST" "http://localhost:8080/auth/signup" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "username": "jeroen",
  "password": "password"
}'

The output of this command looks a whole lot more interesting:

json
{
    "feedToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo",
    "chatToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo",
    "username":"jeroen",
    "userId":"aa66c421332e3bf37bad6edb062ef896"
}

You should get something similar on your backend as well.

Now let's try that login command again.

shell
curl -X "POST" "http://localhost:8080/auth/login" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "username": "jeroen",
  "password": "password"
}'
json
{
    "feedToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo",
    "chatToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYWE2NmM0MjEzMzJlM2JmMzdiYWQ2ZWRiMDYyZWY4OTYifQ.3u7tBgtXq4TeO15sRSsQwlorFim0BQGKJlw3JP2cxJo",
    "username":"jeroen",
    "userId":"aa66c421332e3bf37bad6edb062ef896"
}

It now works.

Notice the chatToken and feedToken. Those are the credentials you need to initialize one of our SDKs from the user side.

Happy coding.

decorative lines
Integrating Video With Your App?
We've built an audio and video solution just for you. Launch in days with our new APIs & SDKs!
Check out the BETA!