Skip to content

astrotars/stream-chat-api

Repository files navigation

No Frills Open-Source API in Node.js for Stream Chat 💬

Overview

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.

Quick Instructions (localhost)

  1. Create an free chat trial with Stream
  2. Clone repo with git clone git@github.com:nparsons08/stream-chat-api.git
  3. Run yarn to install dependencies cd stream-chat-api && yarn
  4. Create a .env file and reference the .env.example file
  5. Start the API with yarn dev for development mode or yarn start for production mode
  6. Use Postman to hit the API with a POST on port 8080 (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"
}

Technology Used

The following technologies were used to build this API:

Support

  • 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.

Requirements

  • Node.js (latest)
  • Yarn (latest)
  • MongoDB (free via MongoDB Atlas)

Deployment

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.

Heroku

The easiest method to deploy this API to Heroku is to click the deploy button below:

Deploy

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 named MONGODB_URI. For local installations, you will need to add your MongoDB URI to the .env file in the root of this project.

Heroku Settings

Docker

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.

Required Environment Variables

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

Authorization

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"
}'

Anatomy

├── 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

Releases

No releases published

Sponsor this project

Packages

No packages published