Initialize 2FA API client:
use Infobip\Api\TfaApi;
use Infobip\Configuration;
$configuration = new Configuration(
host: 'your-base-url',
apiKey: 'your-api-key'
);
$tfaApi = new TfaApi(config: $configuration);
Before sending one-time PIN codes you need to set up application and message template.
The application represents your service. It’s good practice to have separate applications for separate services.
$tfaApplication = $tfaApi->createTfaApplication(
new TfaApplicationRequest(name: '2FA application')
);
$appId = $tfaApplication->getApplicationId();
Message template is the message body with the PIN placeholder that is sent to end users.
$tfaMessageTemplate = $tfaApi
->createTfaMessageTemplate(
$appId,
new TfaCreateMessageRequest(
messageText: 'Your pin is {{pin}}',
pinType: TfaPinType::NUMERIC,
pinLength: 4
)
);
$messageId = $tfaMessageTemplate->getMessageId();
After setting up the application and message template, you can start generating and sending PIN codes via SMS to the provided destination address.
$sendCodeResponse = $tfaApi
->sendTfaPinCodeOverSms(
new TfaStartAuthenticationRequest(
applicationId: $appId,
messageId: $messageId,
to: '41793026727'
from: 'InfoSMS',
)
);
$isSuccessful = $sendCodeResponse->getSmsStatus() === "MESSAGE_SENT";
$pinId = $sendCodeResponse->getPinId();
Verify a phone number to confirm successful 2FA authentication.
$verifyResponse = $tfaApi->verifyTfaPhoneNumber($pinId, new TfaVerifyPinRequest(pin: '1598'));
$verified = $verifyResponse->getVerified();