Data driven phone number needs country code pre-pended

I have an csv file that contains phone number 8005551212 and variable name = {{phone}}. I would like to add a prefix = +1 thru postman rather than edit/reformat/update the csv file.

I need to add prefix +1 in order to pass +18005551212 to an API to send an SMS.

How to I add or hardcode +1 to the variable {{phone}}?

I tried examples:

{
“to”: {{+1}}{{phone}}
“to”: “+1”{{phone}}
“to”: {“+1”{phone}}
}

Please advise.

Is {{phone}} a string or number.

I’m guessing that to add the +1, then it needs to end up as a string?

Have a look at the following topic.

Use environment variable to object property name in tests - Help - Postman

Might need to have square brackets .

HI Team,

I have tried a couple of suggestions to no avail.
Here is what I am attempting with no success.

  1. Import a comma delimited CSV using Run Collection \ Select File \ Import

column heading = Name,Contact
row data = John Doe,8005551212

  1. The Contact value has “8005551212” as a string value (exported from excel as string column).

  2. Body in POST Request:

{
“to” : {{Contact}},
“from” : “+18775551212”,
“text” : “Hey, check this out!”,
“applicationId” : “EnterAppIicationIdHere”,
“tag” : “This is test message”,
“priority” : “default”
}

  1. When the POST is sent I want to be sure to pre-pend “+1” when the CSV is being read and processed by POST
    so that the value of {{Contact}} = “+18005551212”

  2. I am trying this pre-request script with no luck. I think I have the syntax right but getting Property Assignment Expected

const phone = {{Contact}};
pm.globals.set({{Contact}}, “+1” + phone);

or

const phone = pm.get.Contact;
pm.globals.set.Contact, “+” + phone;

You can’t have the same name for the header in the CSV file and the global variable you are trying to set in the pre-request script. (You can but the local scope will win, which will be the CSV file entry). Just call them something else so you know what’s what.

You can reference the entry in the CSV file by prepending data to the variable\header name.

For example.

pm.collectionVariables.set("Phone", "+1" + data.Contact);

One line in your pre-request script which will set a collection variable with your updated phone number that can be used in the request, using the standard methods {{phone}}.

Collection Variable, not Global. (Pretty sure this variable doesn’t need to be seen by all of your collections in Postman).

Please note, you can’t use {{variable}} in the tests tab or pre-request scripts. It only works like that in the body, headers and params section. You either need to use pm.collectionVariables.get(“Phone”) or if referencing a line in the CSV file, by using data.headername

image

image

Postman Echo doesn’t really like the “+1” at the beginning of the string and doesn’t actually return it properly in the response. But hopefully your API will be more accepting.