> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes. # Send a message POST https://%7BYour_Space_Name%7D.signalwire.com/api/messaging/messages Content-Type: application/json Create and queue an outbound message for delivery. The channel is determined by the `from` number: - **SMS/MMS** when `from` is a purchased SignalWire phone number or shortcode. The message is MMS when `media` is present or `send_as_mms` is set, otherwise SMS. - **WhatsApp** when `from` is a `whatsapp:`-prefixed [WhatsApp number](/docs/platform/messaging/whatsapp/send-messages). Set `message_type` for a content message, or `template_id` for an [approved template](/docs/platform/messaging/whatsapp/message-templates). Free-form WhatsApp content is only allowed within the 24-hour customer service window. Use this SignalWire REST operation for JSON requests. Integrations built around form-encoded requests and Message SIDs can use the Compatibility API's [Create message](/docs/compatibility-api/rest/messages/create-message) operation instead. #### Permissions The API token used to authenticate must have the following scope(s) enabled to make a successful request: _Messaging_. [Learn more about API scopes](/docs/platform/your-signalwire-api-space). Reference: https://signalwire.com/docs/apis/rest/messages/create-message ## Authentication - `Authorization` header (basic auth, required) — SignalWire Basic Authentication using Project ID and API Token. The client sends HTTP requests with the Authorization header containing the word Basic followed by a space and a base64-encoded string of project_id:token. The project ID will be used as the username and the API token as the password. Example: ``` Authorization: Basic base64(project_id:token) ``` ## Request ### Body (application/json) This endpoint expects a Message.SendMessageRequest. - `Message.SendMessageRequest` ## Response ### 201 Response returned when a message is successfully created and queued for delivery. - `id` (string, required) — The unique ID of the message. This is the `MessageSegment` ID, consistent with the dashboard and the `/api/messaging/logs` endpoint. - `from` (string, required) — The source phone number. - `to` (string, required) — The destination phone number. - `body` (string, required) — The message body text. Returns an empty string when the message has been redacted. - `status` (enum, required) — Delivery state of the message. - Allowed values: `queued`, `initiated`, `sent`, `delivered`, `undelivered`, `failed`, `read` - `direction` (enum, required) — The direction of the message. - Allowed values: `inbound`, `outbound` - `kind` (enum, required) — The kind of message. - Allowed values: `sms`, `mms`, `whatsapp` - `media` (list of string, required) — Array of URLs for any media attachments on the message. Empty for SMS. - `number_of_segments` (integer, required) — Number of segments the message body was split into for delivery. - `error_code` (string, required, nullable) — Provider-specific error code if delivery failed. Null when no error occurred. - `error_message` (string, required, nullable) — Human-readable error message if delivery failed. Null when no error occurred. - `created_at` (datetime, required) — Date and time when the message was created. - `project_id` (string, required) — The ID of the project the message belongs to. - `status_callback_url` (string, required, nullable) — Callback URL configured to receive message status events. Null if no callback was configured. - `message_uri` (string, required) — Relative URL for retrieving the message via the `/api/messaging/logs` endpoint. ## Errors ### 400 Bad Request Error The request is invalid. - `error` (enum, required) - Allowed values: `Bad Request` ### 401 Unauthorized Error Access is unauthorized. - `error` (enum, required) - Allowed values: `Unauthorized` ### 422 Unprocessable Entity Error The request contains invalid parameters. See errors for details. - `errors` (list of Types.StatusCodes.RestApiErrorItem, required) — List of validation errors. ### 500 Internal Server Error An internal server error occurred. - `error` (enum, required) - Allowed values: `Internal Server Error` ## Types ### Message.CreateMessageRequest Request body for sending a new SMS or MMS message. - `to` (string, required) — Destination phone number in E.164 format (`+` followed by 5-17 digits). Also accepts passthrough numbers like `988`/`+988`. - `from` (string, required) — Source phone number. Must be a purchased SignalWire phone number on the project in E.164 format, or a shortcode (5-6 digits). Verified caller IDs are not permitted. - `body` (string, optional) — Message body text. Required if `media` is not provided. Subject to provider-specific character limits. - `media` (list of string, optional) — Array of HTTP or HTTPS URLs for media attachments. Presence of media makes the message MMS. Maximum 8 items. - `send_as_mms` (boolean, optional, default: false) — Force the message to be sent as MMS even when no media attachments are provided. - `status_callback` (string, optional) — A valid URL to receive message status callback events at each state change. See the [Message status callback](/docs/apis/rest/webhooks/message-status-callback) webhook for the payload your URL will receive. - `custom_variables` (MessageCreateMessageRequestCustomVariables, optional) — Your own key/value string pairs to attach to the message — for example, an order or case number you want to recognize later. When you also set `status_callback`, SignalWire includes these pairs as a `custom_variables` object in every status callback it sends to that URL, so you can match each callback to a record in your own system. If you don't set `status_callback`, there is nowhere for the variables to be delivered. Each value must be a non-empty string of at most 1024 bytes. You can send at most 20 pairs. Each key must start with a letter or underscore and contain only letters, numbers, and underscores, and cannot begin with the reserved prefixes `signalwire_`, `sw_`, `rtc_`, or `internal_` (case-insensitive). Keys are case-sensitive. ### Message.WhatsAppTemplateMessageRequest Send an approved WhatsApp template. Use this to reach a customer for the first time or outside the 24-hour window. Do not include `body` or `message_type`. - `to` (string, required) — Recipient phone number in E.164 format. - `from` (string, required) — Your WhatsApp business number, prefixed with `whatsapp:`. The prefix is what routes the message over WhatsApp instead of SMS/MMS. - `template_id` (string, required) — The template to send, by SignalWire template ID or Meta template ID. The template must be `approved`. - `status_callback` (string, optional) — A valid URL to receive message status callback events at each state change. - `custom_variables` (MessageWhatsAppTemplateMessageRequestCustomVariables, optional) — Your own key/value string pairs to attach to the message. Delivered as `custom_variables` in status callbacks when `status_callback` is set. - `header_template_parameters` (MessageWhatsAppTemplateMessageRequestHeaderTemplateParameters, optional) — Values for the placeholders in the template header. An array for positional parameters, an object for named parameters, or a media URL string for a document/media header. - `body_template_parameters` (MessageWhatsAppTemplateMessageRequestBodyTemplateParameters, optional) — Values for the placeholders in the template body. An array for positional parameters or an object for named parameters. - `button_template_parameters` (list of string, optional) — Values for URL-button placeholders. Positional only (an array); named parameters are not supported for buttons. ### Types.StatusCodes.RestApiErrorItem Details about a specific error. - `type` (string, required) — The category of error. - `code` (string, required) — A specific error code. - `message` (string, required) — A description of what caused the error. - `url` (string, required) — A link to documentation about this error. - `attribute` (string, optional, nullable) — The request parameter that caused the error, if applicable. ### MessageCreateMessageRequestCustomVariables Your own key/value string pairs to attach to the message — for example, an order or case number you want to recognize later. When you also set `status_callback`, SignalWire includes these pairs as a `custom_variables` object in every status callback it sends to that URL, so you can match each callback to a record in your own system. If you don't set `status_callback`, there is nowhere for the variables to be delivered. Each value must be a non-empty string of at most 1024 bytes. You can send at most 20 pairs. Each key must start with a letter or underscore and contain only letters, numbers, and underscores, and cannot begin with the reserved prefixes `signalwire_`, `sw_`, `rtc_`, or `internal_` (case-insensitive). Keys are case-sensitive. ### MessageWhatsAppTemplateMessageRequestCustomVariables Your own key/value string pairs to attach to the message. Delivered as `custom_variables` in status callbacks when `status_callback` is set. ### MessageWhatsAppTemplateMessageRequestHeaderTemplateParameters Values for the placeholders in the template header. An array for positional parameters, an object for named parameters, or a media URL string for a document/media header. ### MessageWhatsAppTemplateMessageRequestBodyTemplateParameters Values for the placeholders in the template body. An array for positional parameters or an object for named parameters. ## Examples **Request** ```json { "from": "+15559876543", "to": "+15551234567" } ``` **Response** ```json { "id": "c2d3e4f5-a6b7-8901-cdef-234567890abc", "from": "+15559876543", "to": "+15551234567", "body": "Your order #12345 has shipped!", "status": "queued", "direction": "outbound", "kind": "sms", "media": [], "number_of_segments": 1, "error_code": null, "error_message": null, "created_at": "2024-05-06T12:20:00Z", "project_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "status_callback_url": null, "message_uri": "/api/messaging/logs/c2d3e4f5-a6b7-8901-cdef-234567890abc" } ``` **SDK Code** ```python import requests url = "https://{your_space_name}.signalwire.com/api/messaging/messages" payload = { "from": "+15559876543", "to": "+15551234567" } headers = { "Content-Type": "application/json" } response = requests.post(url, json=payload, headers=headers, auth=("", "")) print(response.json()) ``` ```javascript const url = 'https://{your_space_name}.signalwire.com/api/messaging/messages'; const credentials = btoa(":"); const options = { method: 'POST', headers: { Authorization: `Basic ${credentials}`, 'Content-Type': 'application/json' }, body: '{"from":"+15559876543","to":"+15551234567"}' }; try { const response = await fetch(url, options); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } ``` ```go package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://{your_space_name}.signalwire.com/api/messaging/messages" payload := strings.NewReader("{\n \"from\": \"+15559876543\",\n \"to\": \"+15551234567\"\n}") req, _ := http.NewRequest("POST", url, payload) req.SetBasicAuth("", "") req.Header.Add("Content-Type", "application/json") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` ```ruby require 'uri' require 'net/http' url = URI("https://{your_space_name}.signalwire.com/api/messaging/messages") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request.basic_auth("", "") request["Content-Type"] = 'application/json' request.body = "{\n \"from\": \"+15559876543\",\n \"to\": \"+15551234567\"\n}" response = http.request(request) puts response.read_body ``` ```java import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://{your_space_name}.signalwire.com/api/messaging/messages") .basicAuth("", "") .header("Content-Type", "application/json") .body("{\n \"from\": \"+15559876543\",\n \"to\": \"+15551234567\"\n}") .asString(); ``` ```php request('POST', 'https://{your_space_name}.signalwire.com/api/messaging/messages', [ 'body' => '{ "from": "+15559876543", "to": "+15551234567" }', 'headers' => [ 'Content-Type' => 'application/json', ], 'auth' => ['', ''], ]); echo $response->getBody(); ``` ```csharp using RestSharp; using RestSharp.Authenticators; var client = new RestClient("https://{your_space_name}.signalwire.com/api/messaging/messages"); client.Authenticator = new HttpBasicAuthenticator("", ""); var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{\n \"from\": \"+15559876543\",\n \"to\": \"+15551234567\"\n}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` ```swift import Foundation let credentials = Data(":".utf8).base64EncodedString() let headers = [ "Authorization": "Basic \(credentials)", "Content-Type": "application/json" ] let parameters = [ "from": "+15559876543", "to": "+15551234567" ] as [String : Any] let postData = JSONSerialization.data(withJSONObject: parameters, options: []) let request = NSMutableURLRequest(url: NSURL(string: "https://{your_space_name}.signalwire.com/api/messaging/messages")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ```