DEVELOPER GUIDE

How to Send SMS from Laravel with the Grupo Tecnophone API

Add SMS sending to your Laravel application with the framework’s HTTP client, centralized configuration and queues to process sends in the background.

Before you start

  • An API connection created in the web panel (app.grupotecnophone.com), with its Bearer Token for sandbox or production.
  • Your server’s public IP registered as authorized for that token.
  • A User-Agent header that identifies your application, for example MyCompany/1.0.
  • Laravel 9 or later (the Http client is included) and a queue system configured if you will send in the background.

Store the token in an environment variable (in these examples, GTP_SMS_TOKEN) and never include it in the code of a mobile app or website: API calls must always come from your backend, from an authorized IP.

Send an SMS with Laravel

1. Configuration. Add the credentials to config/services.php and define GTP_SMS_TOKEN in your .env:

// config/services.php
'gtp_sms' => [
    'token'    => env('GTP_SMS_TOKEN'),
    'endpoint' => env('GTP_SMS_ENDPOINT', 'https://api.grupotecnophone.com/prod/v1/sms/send'),
],

2. Service. Centralize sending in a reusable class:

<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;
use RuntimeException;

class SmsService
{
    public function send(string $to, string $body): array
    {
        $response = Http::withToken(config('services.gtp_sms.token'))
            ->withHeaders(['User-Agent' => 'MiEmpresa/1.0'])
            ->acceptJson()
            ->timeout(30)
            ->post(config('services.gtp_sms.endpoint'), [
                'to'   => $to,
                'body' => $body,
            ]);

        $data = $response->json() ?? [];

        if ($response->successful() && ($data['status'] ?? null) === 'success') {
            return $data; // sid, encoding, num_chars, num_segments
        }

        $code = $data['error']['code'] ?? 'HTTP_' . $response->status();

        throw new RuntimeException($code . ': ' . ($data['error']['message'] ?? $response->body()));
    }
}

3. Queued sending. To avoid blocking the web request —and to absorb sending peaks— dispatch a job:

<?php

namespace App\Jobs;

use App\Services\SmsService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class SendSms implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 1; // avoids duplicate resends on network errors

    public function __construct(public string $to, public string $body)
    {
    }

    public function handle(SmsService $sms): void
    {
        $result = $sms->send($this->to, $this->body);

        Log::info('SMS aceptado', ['sid' => $result['sid'], 'segmentos' => $result['num_segments']]);
    }
}

// Desde un controlador o un listener:
// SendSms::dispatch('+525512345678', 'Your verification code is 482913. It expires in 5 minutes.');

API response

If the message was accepted, the API responds with HTTP 200 and a JSON body like this:

{
  "sid": "sms_20261012143201_39bdafbd",
  "status": "success",
  "to": "+525512345678",
  "body": "Your verification code is 482913. It expires in 5 minutes.",
  "encoding": "GSM7",
  "num_chars": 56,
  "num_segments": 1,
  "error": null
}

Store the sid: it is the identifier you will use to reconcile the message’s delivery report. The num_segments field indicates how many segments will be billed; see how segments are calculated.

Common errors

HTTPCodeMeaning
400ENCRYPTION_REQUIREDThe connection requires to and body to be encrypted with RSA-OAEP
400ENCRYPTION_NOT_ALLOWEDThe connection operates in plain text and encrypted content was received
400INPUT_PHONE_BLACKLISTEDThe destination number is on the blacklist
400INSUFFICIENT_BALANCEInsufficient balance for the send
401—Missing or invalid token
403—The source IP is not authorized for the token
500—Unexpected internal error

On 400 errors the response includes an error object with code, code_num and message.

Test in sandbox

Change the endpoint to https://api.grupotecnophone.com/test/v1/sms/send and use the sandbox token. That environment validates the token, IP, payload and encryption with the same logic as production, but does not send real SMS.

Best practices

  • Set a timeout and log each response with its sid.
  • After a network error with no response, do not resend blindly: check first so you don’t duplicate the message to the user.
  • Retry only 500 errors, with progressive backoff and a maximum number of attempts.
  • Queue bulk sends instead of sending them within a web request cycle.
  • If the content is sensitive, enable connection encryption; the API documentation includes an RSA encryption example in PHP without external libraries.

See also the SMS API for Mexico, the SMPP connection and the guides for PHP, Python, Node.js and Java. If you need test credentials, contact us.

FAQ: Sending SMS with Laravel

Scroll to Top