This API will quickly get you up to speed on best practices for deploying an auth API for Stream Chat. The API can be deployed Heroku with a single click or to any other hosting environment that supports Node.js.
- Create an free chat trial with Stream
- Clone repo with
git clone git@github.com:nparsons08/stream-chat-api.git
- Run yarn to install dependencies
cd stream-chat-api && yarn
- Create a
.env
file and reference the.env.example
file - Start the API with
yarn dev
for development mode oryarn start
for production mode - Use Postman to hit the API with a
POST
on port8080
(e.g.http://localhost:8080/v1/auth/init
) using a variation of the following JSON payload:
{
"name": {
"first": "First Name",
"last": "Last Name"
},
"email": "foo@bar.baz",
"password": "qux"
}
The following technologies were used to build this API:
- User storage via MongoDB database
- Mongoose schema with validation for user profiles
- Password validation and hashing with bcrypt
- Find or create for users within the MongoDB database
- Easy deployment to Heroku (optional) or any other environment
- Token generation for existing and new users (for Stream Chat)
- Creation of a new channel named
General
if one does not exist - Automatic adding of users to the
General
channel - Heavily commented modern JavaScript
Please see below for installation requirements.
- Node.js (latest)
- Yarn (latest)
- MongoDB (free via MongoDB Atlas)
This section covers the various requirements for deploying this API in different environments. When in doubt, have a look at the .env.example
file which outlines what required variables you will need to supply in order for the API to run properly.
The easiest method to deploy this API to Heroku is to click the deploy button below:
Note: You will need to spin up a MongoDB cluster and add your MongoDB URI to your environment variables under the
Settings
section of your applications dashboard on Heroku. Ensure that the environment variable is namedMONGODB_URI
. For local installations, you will need to add your MongoDB URI to the.env
file in the root of this project.
Note: A Dockerfile is also available with associated scripts located in the
scripts
directory. Use this if you would like to deply to AWS/GCP/Kubernetes.
Note: Be sure to add your environment variables as shown below and in the
.env.example
file or your build will fail.
NODE_ENV=development
PORT=8080
AUTH_KEY=YOUR_AUTH_KEY
STREAM_API_KEY=YOUR_STREAM_API_KEY
STREAM_API_SECRET=YOUR_STREAM_API_SECRET
MONGODB_URI=YOUR_MONGODB_URI
To make calls to the API, you must send an Authorization
header with your AUTH_KEY
which is either a) provided by you, or b) generated by Heroku automatically. Below is an example:
curl --location --request POST 'http://localhost:8080/v1/auth/init' \
--header 'Content-Type: application/json' \
--header 'Authorization: YOUR_AUTH_KEY' \
--data-raw '{
"name": {
"first": "First Name",
"last": "Last Name"
},
"email": "foo@bar.baz",
"password": "qux"
}'
├── controllers
│ └── v1
│ └── auth
│ ├── index.js
│ └── init.action.js
├── index.js
├── models
│ └── user.js
├── routes
│ └── init.js
└── utils
├── auth
│ └── index.js
├── controllers.js
└── db
└── index.js