Automating Sending Client Intake Forms with C#

Osmosis Support posted this on api

The Osmosis API allows you to send a customer a form when they initiate contact with you. To do this, you minimally need the customer's email, although you can put all the information you have in when you create the client.

The API accepts a JSON payload, containing customer information:

var customerEmail = "support@getosmosis.com";
var customerFirstName = "Bob";
var customerLastName = "Loblaw";
var apiKey = "";
var apiKeyParam = "?key=";
var baseUrl = "https://www.getosmosis.com/";

var createClientApi = "api/Clients/Create";
            
var createClient = @"{
""Email"": """ + customerEmail + @""",
""FirstName"": """ + customerFirstName + @""",
""LastName"": """ + customerLastName + @""",
""Gravatar"": """ + customerEmail + @""",
""Name"": """ + customerFirstName + " " + customerLastName + @""",
""Notes"": ""created by backend trial process""
}";
var clientId = getResult(baseUrl + createClientApi + apiKeyParam + apiKey, createClient);

The API returns you a customer ID, which you can use for further API calls, such as sending a questionnaire. If you use this API with a questionnaire ID, the API will automatically copy the questionnaire and send the copy to the client.

var sentQuestionnaireApi = "api/Questionnaires/Send";
var sentQuestionnaire = @"
{
""ClientId"": " + clientId + @",
""QuestionnaireDraftId"": ,
""Message"": 'Customer Satisfaction Survey Demo""
}";

var sentQuestionnaireResult = getResult(baseUrl + sentQuestionnaireApi + apiKeyParam + apiKey, sentQuestionnaire);
To actually implement this, you currently need some type of JSON API- you can use the code below to orchestrate this process.
private static String getResult(string url, string postData)
{
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";
            
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/json";
    request.ContentLength = byteArray.Length;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
            
    WebResponse response = request.GetResponse();
    dataStream = response.GetResponseStream();
            
    StreamReader reader = new StreamReader(dataStream);
    string jsonResponse = reader.ReadToEnd();

    OsmosisResponse deserialized = 
        JsonConvert.DeserializeObject(jsonResponse);

    reader.Close();
    dataStream.Close();
    response.Close();

    if (deserialized != null)
    {
        return deserialized.Message;
    }
    else
    {
        return null;
    }
}

14-day free trial. No credit card required. Sign Up for a Free Trial!