Skip to main content
Glama
mmarqueti

WhatsApp Cloud API MCP Server

by mmarqueti
client.ts3 kB
import axios, { AxiosInstance } from 'axios'; import { config } from '../config/env.js'; export class WhatsAppClient { private client: AxiosInstance; constructor() { this.client = axios.create({ baseURL: 'https://graph.facebook.com/v18.0', headers: { 'Authorization': `Bearer ${config.META_WHATSAPP_TOKEN}`, 'Content-Type': 'application/json', }, }); } async sendText(to: string, text: string) { try { const response = await this.client.post(`/${config.META_WHATSAPP_PHONE_ID}/messages`, { messaging_product: 'whatsapp', recipient_type: 'individual', to, type: 'text', text: { body: text }, }); return response.data; } catch (error: any) { console.error('Error sending text message:', error.response?.data || error.message); throw error; } } async sendTemplate(to: string, templateName: string, language: string, components: any[] = []) { try { const response = await this.client.post(`/${config.META_WHATSAPP_PHONE_ID}/messages`, { messaging_product: 'whatsapp', recipient_type: 'individual', to, type: 'template', template: { name: templateName, language: { code: language }, components, }, }); return response.data; } catch (error: any) { console.error('Error sending template message:', error.response?.data || error.message); throw error; } } async getMediaUrl(mediaId: string): Promise<string> { try { const response = await this.client.get(`/${mediaId}`); return response.data.url; } catch (error: any) { console.error('Error getting media URL:', error.response?.data || error.message); throw error; } } async downloadMedia(url: string): Promise<Buffer> { try { const response = await this.client.get(url, { responseType: 'arraybuffer', headers: { 'Authorization': `Bearer ${config.META_WHATSAPP_TOKEN}`, }, }); return response.data; } catch (error: any) { console.error('Error downloading media:', error.response?.data || error.message); throw error; } } async healthCheck(): Promise<boolean> { try { await this.client.get(`/${config.META_WHATSAPP_PHONE_ID}`); return true; } catch (error: any) { console.error('Health check failed:', error.response?.data || error.message); return false; } } } export const whatsAppClient = new WhatsAppClient();

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mmarqueti/whatsapp-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server