Examples

Contents

PHP

Install the PHP library using Composer:

composer require stackmash/stackmash-php

Use the Stackmash namespace:

use Stackmash\Stackmash;

Get the project you want to send notification to using your project keys:

$project = Stackmash::getProject("YOUR_PUBLIC_KEY", "YOUR_PRIVATE_KEY");

Create a notification:

$project->action($category, $title, $body);
Configuration options

By default, the browser, OS and IP address of the users is not sent to the servers. If you would like to enable this, you can parse configuration settings to the project using the following example:

$project = Stackmash::getProject("YOUR_PUBLIC_KEY", "YOUR_PRIVATE_KEY", $config);

The config settings can be defined like this, setting true of false depending on what you would like to send to the Stackmash servers:

$config = ['showBrowser' => false, 'showOs' => false, 'showIp' => false];

Node

For NodeJS we strongly recommend installing the request module. You can do this by running:

npm i request

Once installed, you will need to include the request module into your Node app:

var request = require('request');

To send a request to Stackmash you first need to have your parameters ready:

var public_key = 'YOUR_PUBLIC_KEY';
var private_key = 'YOUR_PRIVATE_KEY';

var category = 'user-registration';
var title = 'You have a new customer!';
var body = {
	"Email address": "john.doe@gmail.com",
	"Name": "John Doe",
	"Date of birth": "19/03/1986",
	"Interests": [
		"Rocket building", "Football", "Programming"
	]
};

Then use the following code to setup the correct options for the request:

var options = {
	url: 'https://api.stackmash.com/api/notification/create',
	method: 'POST',
	headers: {
		'Accept': 'application/json',
		'Content-Type': 'application/json'
	},
	form: {
		public_key: public_key,
		private_key: private_key,
		category: category,
		title: title,
		body: JSON.stringify(body)
	}
};

You can then make the request to the server:

request(options, function (error, response, body)
{
	// If there is no error and we get a 201 created code
	if(!error && response.statusCode == 201)
	{
		// You can also turn the JSON string into a Javascript object
		console.log(JSON.parse(body));
	}
});

.NET

For use with .NET, we strongly recommend installing the RestSharp NuGet package to make sending post requests to the Stackmash servers easier. Once you have installed the package, you will need the following declaration:

using RestSharp;

Once you have this, define your variables as strings:

string public_key = "YOUR_PUBLIC_KEY";
string private_key = " YOUR_PRIVATE_KEY";
string category = " user-registration";
string title = "You have a new customer!";
string body = "{\"Email address\":\"john.doe @gmail.com\",\"Name\":\"John Doe\",\"Date of birth\":\"19/03/1986\",\"Interests\":[\"Rocket building\",\"Football\",\"Programming\"]}";

Then define the client and request, this code does not need to be altered:

var client = new RestClient("https://api.stackmash.com");
var request = new RestRequest("api/notification/create", Method.POST);

Then we set our headers:

request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");

The parameters are then added to the body:

request.AddJsonBody(new { public_key = public_key, private_key = private_key, category = category, title = title, body = body });

Finally, the request can be executed:

IRestResponse response = client.Execute(request);

You can then take the response and view it by running:

Console.WriteLine(response.Content);

If you would like to know more about the RestSharp package, the documentation can be viewed here.


Python

Integrating Stackmash with Python is super simple. First you will need the requests library to send a request to the Stackmash servers. To do this you will need to run either:

pip install requests

Or:

pipenv install requests

You can learn more about installing requests by clicking here. Once installed you will need to import the library:

import requests

Then assign your parameters:

public_key = 'YOUR_PUBLIC_KEY'
private_key = 'YOUR_PRIVATE_KEY'
category = 'user-registration'
title = 'You have a new customer!'
body = '{"Email address":"john.doe @gmail.com","Name":"John Doe","Date of birth":"19/03/1986","Interests":["Rocket building","Football","Programming"]}'

Then create the parameters for the request:

url = "https://api.stackmash.com/api/notification/create"
data = {"public_key": public_key, "private_key": private_key, "category": category, "title": title, "body": body}
headers = {"Accept": "application/json"}

And then send the request:

response = requests.post(url, data = data, headers = headers)

To view the response you can add this:

print(response.text)

And that’s it! To find out more about the requests library click here.


Other

These docs have covered how you can send a simple POST request to the Stackmash servers in various programming languages, but we haven’t covered them all. If your favourite language was not listed here, please contact support, we may be able to help. Alternatively, you can search online for information on how you can send post requests using whatever language you want to use.

We are constantly adding to these docs to make them as concise as possible, so keep checking for any updates. If you want to contribute to these docs, please contact support and let us know! We love getting your feedback as it not only helps use, but most importantly, other users of Stackmash.