> 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. # Create a Message POST https://YOUR_SPACE.signalwire.com/api/laml/2010-04-01/Accounts/{AccountSid}/Messages Content-Type: application/x-www-form-urlencoded Sends a message through the account and creates a Message resource for tracking its delivery. Use this form-encoded route for SID-based integrations; use SignalWire REST [Send a message](/docs/apis/rest/messages/create-message) for a JSON request. #### 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/compatibility-api/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 ### Path parameters - `AccountSid` (string, required) — The unique identifier of the project that sent or received this message. ### Body (application/x-www-form-urlencoded) This endpoint expects a CreateMessageRequest. - `To` (string, required) — The phone number in E.164 format that will receive the message. - `From` (string, optional) — The phone number in E.164 format that will send the message. Either `From` or `MessagingServiceSid` must be provided. - `Body` (string, optional) — The text of the message. Either `Body` or `MediaUrl` must be provided. - `MediaUrl` (CreateMessageRequestMediaUrl, optional) — URL(s) of media you wish to attach and send with the message. Can be specified multiple times for up to 8 media items. - `SendAsMms` (boolean, optional) — When set to true, forces the message to be sent as an MMS. - `ApplicationSid` (string, optional) — The SID of a SignalWire cXML Application used to configure the message's status callback. - `MaxPrice` (string, optional) — The maximum price in USD acceptable for the message to be sent. Format: decimal with up to 4 decimal places. - `StatusCallback` (string, optional) — A URL endpoint to receive callbacks each time the status of the message changes. See the [SMS status callback](/docs/compatibility-api/rest/messages/webhooks/sms-status-callback) webhook for the payload your URL will receive. - `ValidityPeriod` (integer, optional, default: 14400) — The number of seconds a message will allow being queued before canceling. Must be between 1 and 172800 (48 hours). Default is 14400 (4 hours). - `MessagingServiceSid` (string, optional) — The ID of a number group to use when sending the message. Either `From` or `MessagingServiceSid` must be provided. ## Response ### 201 Response returned when a message is created. - `account_sid` (string, required) — The unique identifier of the project that sent or received this message. - `api_version` (string, required) — The version number of the SignalWire cXML REST API used to handle this message. - `body` (string, required, nullable) — The text of the message. Up to 1600 characters long. May be null if filtered for spam. - `num_segments` (integer, required) — The number of segments that make up the entire message. - `num_media` (integer, required) — The number of media files that were included with the message. - `date_created` (string, required) — The date and time the message was created in RFC 2822 format. - `date_sent` (string, required, nullable) — The date and time the message was sent in RFC 2822 format, or null if not yet sent. - `date_updated` (string, required) — The date and time the message was last updated in RFC 2822 format. - `direction` (enum, required) — The direction of the message. - Allowed values: `inbound`, `outbound-api`, `outbound-call`, `outbound-reply` - `error_code` (string, required, nullable) — If an error has occurred on the message, the error code will give you a specific code, or null if no error. - `error_message` (string, required, nullable) — A human readable description of the error that occurred, or null if no error. - `from` (string, required) — The phone number in E.164 format that sent the message. - `price` (float, required, nullable) — The cost of the individual message billed to your project, or null if not yet calculated. - `price_unit` (string, required) — The currency in which `price` is charged as. - `sid` (string, required) — A unique ID that identifies this specific message. - `status` (enum, required) — Current status of the message. - Allowed values: `queued`, `initiated`, `sent`, `failed`, `delivered`, `undelivered`, `received` - `to` (string, required) — The phone number in E.164 format that received the message. - `messaging_service_sid` (string, required, nullable) — If a number group was used when sending an outbound message, the number group's ID will be present, or null otherwise. - `uri` (string, required) — The URI of this particular message. - `subresource_uris` (MessageSubresourceUris, required) — The URIs for any subresources associated with this message. ## Errors ### 400 Bad Request Error The request was invalid or cannot be processed. Check the error details for more information. - `code` (integer, required) — Error code. - `message` (string, required) — Error message. - `more_info` (string, required) — URL for more information about the error. - `status` (integer, required) — HTTP status code. ### 401 Unauthorized Error Authentication failed. Please verify your credentials and try again. - `code` (integer, required) — Error code. - `message` (string, required) — Error message. - `more_info` (string, required) — URL for more information about the error. - `status` (integer, required) — HTTP status code. ### 422 Unprocessable Entity Error The request could not be processed due to validation errors. Check the error details for more information. - `code` (integer, required) — Error code. - `message` (string, required) — Error message. - `more_info` (string, required) — URL for more information about the error. - `status` (integer, required) — HTTP status code. ## Types ### CreateMessageRequestMediaUrl URL(s) of media you wish to attach and send with the message. Can be specified multiple times for up to 8 media items. ### MessageSubresourceUris Message subresource URIs. - `media` (string, required) — The URI for media. ## Examples **Request** ```json { "To": "+15557654321" } ``` **Response** ```json { "account_sid": "ea108133-d6b3-407c-9536-9fad8a929a6a", "api_version": "2010-04-01", "body": "Hello World!", "num_segments": 1, "num_media": 1, "date_created": "Mon, 13 Aug 2018 21:38:46 +0000", "date_sent": "Mon, 13 Aug 2018 21:38:46 +0000", "date_updated": "Mon, 13 Aug 2018 21:38:46 +0000", "direction": "inbound", "error_code": "30001", "error_message": "Queue overflow", "from": "+15551234567", "price": 0.005, "price_unit": "USD", "sid": "0a059168-ead0-41af-9d1f-343dae832527", "status": "queued", "to": "+15557654321", "messaging_service_sid": "b3877c40-da60-4998-90ad-b792e98472ms", "uri": "/api/laml/2010-04-01/Accounts/ea108133-d6b3-407c-9536-9fad8a929a6a/Messages/0a059168-ead0-41af-9d1f-343dae832527.json", "subresource_uris": { "media": "/api/laml/2010-04-01/Accounts/ea108133-d6b3-407c-9536-9fad8a929a6a/Messages/0a059168-ead0-41af-9d1f-343dae832527/Media.json" } } ``` **SDK Code** ```python import requests url = "https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages" payload = "" headers = { "Content-Type": "application/x-www-form-urlencoded" } response = requests.post(url, data=payload, headers=headers, auth=("", "")) print(response.json()) ``` ```javascript const url = 'https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages'; const credentials = btoa(":"); const options = { method: 'POST', headers: { Authorization: `Basic ${credentials}`, 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams('') }; 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" "net/http" "io" ) func main() { url := "https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages" req, _ := http.NewRequest("POST", url, nil) req.SetBasicAuth("", "") req.Header.Add("Content-Type", "application/x-www-form-urlencoded") 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.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/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/x-www-form-urlencoded' 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.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages") .basicAuth("", "") .header("Content-Type", "application/x-www-form-urlencoded") .asString(); ``` ```php request('POST', 'https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages', [ 'form_params' => null, 'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded', ], 'auth' => ['', ''], ]); echo $response->getBody(); ``` ```csharp using RestSharp; using RestSharp.Authenticators; var client = new RestClient("https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages"); client.Authenticator = new HttpBasicAuthenticator("", ""); var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); IRestResponse response = client.Execute(request); ``` ```swift import Foundation let credentials = Data(":".utf8).base64EncodedString() let headers = [ "Authorization": "Basic \(credentials)", "Content-Type": "application/x-www-form-urlencoded" ] let request = NSMutableURLRequest(url: NSURL(string: "https://your_space.signalwire.com/api/laml/2010-04-01/Accounts/AccountSid/Messages")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.allHTTPHeaderFields = headers 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() ```