You can use the contacts API for this. The below code works for me.
However, there are few things you need to do first, create an ADD Keyword and opt in response
and activate it. Send a test sms to a mobile number. Query the SMS Message Tracking DV and get the keywordid for the add keyword. I do not know any other method of finding the keyword id. (May be SF could help).
You need to have all your contacts along with their mobile number in one data extension and then iterate through all records using a for loop in SSJS
Use the below payload and API:
var OPT_IN_STATUS = 2;
var OPT_OUT_STATUS = 0;
var bodyPayload = {
"contactKey": contact.CONTACT_KEY,
"attributeSets": [{
"name": "MobileConnect Subscriptions",
"items": [{
"values": [
{ "name": "Keyword", "value": keywordid},
{ "name": "Mobile Number", "value": MOBILE_NUMBER },
{ "name": "Opt In Status", "value": OPT_IN_STATUS },
{ "name": "Opt Out Status", "value": OPT_OUT_STATUS },
{ "name": "Modified Date", "value": TODAY },
{ "name": "Created Date", "value": TODAY }
]
}]
}]
};
PATCH/https://yourdomainbaseurl.rest.marketingcloudapis.com/contacts/v1/contacts