Zami is the chat API for developers. To start sending or receiving messages you must first connect an account on one of our supported networks.
Sends a text message to a recipient
import { Zami } from 'zami';
const zami = new Zami('om-secret-abcdefghijklmnopqrstuvwxyz');
zami.sendText({
connection_id: '<connection_id>',
recipient: 'zamiapi',
body: 'Hello from Zami',
})
Sends a media message to a recipient. Media needs to be uploaded before calling this endpoint
import { Zami } from 'zami';
const zami = new Zami('om-secret-abcdefghijklmnopqrstuvwxyz');
zami.sendMedia({
connection_id: '<connection_id>',
recipient: 'zamiapi',
media_id: '<media_id>',
});
Uploads media and returns a media ID. You can use this ID to send media to recipients across supported networks.
import { Zami } from 'zami';
const zami = new Zami('om-secret-abcdefghijklmnopqrstuvwxyz')
async function upload() {
const buffer = await fs.readFile('path/to/your/file.png');
const mediaId = await zami.uploadMedia({
media: buffer,
content_type: 'image/png',
});
console.log('Uploaded media id is ', mediaId);
}
upload();
Downloads media given media ID. Media IDs are received via webhook when receiving media messages
import { Zami } from 'zami';
const zami = new Zami('om-secret-abcdefghijklmnopqrstuvwxyz');
async function download() {
const buffer = await zami.downloadMedia('<media_id>');
await fs.writeFile('path/to/your/file.png', buffer);
}
download();
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse JSON bodies
app.use(bodyParser.json());
app.post('/process-zami-webhook', (req, res) => {
if (req.body.name === 'message.created') {
console.log(
'New message',
req.body.connection_id,
req.body.object.id,
)
}
res.send('OK');
});
app.listen(port, () => {
console.log(`Server running on ${port}`);
});
You can use the client library in your web application to let your users connect their chat accounts in order to obtain API access to them.
Include the following script tag on every page of your site
<script src="https://cdn.zamiapi.com/link.js" />
Opens a modal that lets users pick their network and authenticate their accounts. The result is a connection object which you can use to send and receive messages
Zami.createConnection({
publicToken: 'LOG IN TO SEE YOUR PUBLIC TOKEN',
network: 'whatsapp',
onSuccess: (connectionId) => {
alert('success. connection id: ' + connectionId);
},
onExit: () => {
alert('the user closed the modal');
},
})