Server-Side API

Last Edit: Feb 02 2020

The REST API documentation is considered for advanced developers. It should only be used when a server-side client is not available. The full REST documentation is located here.

Server-side API Usage

Here’s an example of sending a chat message from a server-side integration:


const client = new StreamChat('STREAM_KEY', 'STREAM_API_SECRET', {});
const channel = client.channel('team', 'spacex', {
    image: image,
    created_by: elon,
});

const response = await channel.create();

const text = 'I was gonna get to mars but then I got high';
const message = {
    text,
    user: elon,
}

const response = await channel.sendMessage(message);
                    

# gem install stream-chat-ruby

require 'stream-chat'

client = StreamChat::Client.new(api_key='STREAM_KEY', api_secret='STREAM_API_SECRET')
spacex_channel = client.channel("team", channel_id: "spacex", data: {'image'=> image})
spacex_channel.create('elon')


spacex_channel.send_message({'text' => 'I was gonna get to mars but then I got high'}, 'elon')
                    

# pip install stream-chat

from stream_chat import StreamChat

client = StreamChat(api_key="STREAM_KEY", api_secret="STREAM_API_SECRET")
spacex_channel = client.channel('team', 'spacex', {
    "image": image,
});
spacex_channel.create("elon")

spacex_channel.send_message(
    {"text": "I was gonna get to mars but then I got high"},
    user_id="elon"
)
                    

// at the moment we don't have a Java client for server side usage
                    

// go get github.com/GetStream/stream-chat-go

import stream "github.com/GetStream/stream-chat-go"

client, _ := stream.NewClient("STREAM_KEY", []byte("STREAM_API_SECRET"))

spacexChannel, _ := client.CreateChannel("team", "spacex","elon", map[string]interface{}{
    "image": image,
})

message := stream.Message{
    Text: "I was gonna get to mars but then I got high",
}

channel.SendMessage(&message, "elon")
                    

// nuget install stream-chat-net

using StreamChat;

var client = new Client("STREAM_KEY", "STREAM_API_SECRET");
var spacexChannel = client.Channel("team", "spacex");
spacexChannel.Create('elon');

var message = new MessageInput()
{
    Text = "I was gonna get to mars but then I got high",
};
spacexChannel. SendMessage(message, 'elon');
                    

// at the moment we don't have a Swift client for server side usage
                    

//composer require get-stream/stream-chat

$client = new GetStream\StreamChat\Client("STREAM_KEY", "STREAM_API_SECRET");
$spacexChannel = $client->Channel("team", "spacex", ['image' => image]);
$spacexChannel->create('elon');

$spacexChannel->sendMessage(["text" => "I was gonna get to mars but then I got high"], 'elon');
                    

Note the 3 differences compared to the client side integration:

  1. Initialize the client with the server side secret.

  2. Create the Channel instead of initializing it (we don't want to retrieve the state or subscribe to changes in this case).

  3. Pass the user parameter if the endpoint requires it (eg. creating a channel or a message)


const tommaso = { id: "tommaso" };

// create a channel and assign user Tommaso as owner
const channel = client.channel("team", "stream", { created_by: tommaso })
await channel.create();

// send a message for tommaso
const message = {
   text: "hi there!",
   user: tommaso,
};

await channel.sendMessage(message);
                    

# create a channel and assign user Tommaso as owner
channel = client.channel("team", "stream")
channel.create("tommaso")

# send a message for tommaso
message = {
    "text": "hi there!",
    "user": "tommaso"
}
channel.send_message(message, "tommaso")
                    

$tommaso = [ $id => "tommaso" ];

// create a channel and assign user Tommaso as owner
$channel = $client->Channel("team", "stream", [ 'created_by' => 'tommaso' ]);
$channel->create('tommaso');

// send a message on behalf of tommaso
$message = [
   'text'=> "hi there!"
];

$response = $channel->sendMessage($message, 'tommaso');
                    

Server-Side API Clients

Stream Chat provides several server-side API clients for ease of use. The following server-side API clients are available to download via their respective package managers:

  1. Python

  2. Node.js

  3. PHP

  4. Go

  5. Ruby

  6. .Net

For a comprehensive list of both server-side and client-side clients, please reference the GitHub repository located here.