---
updatedAt: 2025-06-09T23:21:12.000Z
agentTools:
  projectIndex: https://docs.ngpvan.com/llms.txt
---

# Modify a Canvass Response to Update SMS Opt-In Status

```node Node
const https = require('https');

const username = 'yourApiUserName';
constapiKey = 'yourApiKey';
constdbMode = '1';

const siteName = api.securevan.com
const password = apiKey + '|' + dbMode;

const standardHeadersAndAuth = {
  accept: 'application/json',
  'content-type': 'application/json',
  authorization: 'Basic ' + Buffer.from(`${username}:${apiKey + '|'+ dbMode}`).toString("base64")
}

let referenceId;
let canvassResultId;

const vanId = 12345; 

const postOptions = {
  method: 'POST',
  hostname: siteName,
  port: null,
  path: `/v4/people/${vanId}/canvassResponses`,
  headers: standardHeadersAndAuth
};

const postRequestBody = {
  contactTypeId: 37, // SMS contactTypeId
  canvassContext: {
    phone: {
      phoneNumber: '(212) 555-1234' 
    }
  },
  responses: {
    type: 'SurveyResponse',
    action: 'Apply',
    surveyQuestionId: 1,
    surveyResponseId: 1
  },
  includeReferenceIdInResponse: true 
};
  
const postRequest = https.request(postOptions, (postResponse) =>  {
  const postResponseChunks = [];
  postResponse.on('data', (chunk) => {
    postResponseChunks.push(chunk);
  });
  postResponse.on('end', () => {
    const postResponseBody = JSON.parse(Buffer.concat(postResponseChunks).toString());
    referenceId = postResponseBody.referenceId;
  });
});
postRequest.write(JSON.stringify(postRequestBody));
postRequest.end();

const getOptions = {
  method: 'GET',
  hostname: siteName,
  port: null,
  path: `/v4/people/${vanId}/canvassResponses/${referenceId}`,
  headers: standardHeadersAndAuth
};

const getRequest = https.request(options, (getResponse) =>  {
  const getResponseChunks = [];
  getResponse.on('data', (chunk) => {
    getResponseChunks.push(chunk);
  });
  getResponse.on('end', () => {
    const getResponseBody = JSON.parse(Buffer.concat(getResponse).toString());
    canvassResultId = getResponseBody.canvassResultId;
  });
});
getRequest.end();

const patchOptions = {
  method: 'PATCH',
  hostname: siteName,
  port: null,
  path: `/v4/people/${vanId}/canvassResponses/${canvassResultId}`,
  headers: standardHeadersAndAuth
};

const patchRequestBody = {
  canvassResultId: canvassResultId,
  canvassContext: {
    phone: {
      phoneNumber: '(212) 555-1234',
      optInStatus: 'I'
    }
  }
};

const patchRequest = https.request(options, (patchResponse) =>  {
  const patchChunks = [];
  patchResponse.on('data', (chunk) => {
    patchChunks.push(chunk);
  });
  patchResponse.on('end', () => {
    const patchResponseBody = JSON.parse(Buffer.concat(patchChunks).toString());
    console.log(patchResponse.statusCode);
  });
});
patchRequest.write(JSON.stringify(requestBody));
patchRequest.end();
```

# Construct a POST request to add a new canvass response

<!-- node@21-56 -->

Since we want to update SMS opt-in status later, we need to include a phone number. This can be either a new or existing phone number for the contact.

# Make sure to set IncludeReferenceIdInResponse to true

<!-- node@42 -->

If this parameter is not set, you will not be able to make a PATCH request!

# Retrieve the referenceId from the POST response body

<!-- node@52 -->



# Construct a GET Request to obtain the new canvassResponseId

<!-- node@58-78 -->

This example calls the GET endpoint right after the POST for simplicity, but in practice, it can take up to 30 minutes for a newly created canvass response to become available via the GET endpoint.

# Retrieve the canvassResultId from the GET response body

<!-- node@73 -->



# Construct a PATCH request to change the opt-in status of the phone number

<!-- node@78-107 -->

We're including the same phone number we used earlier, but we're also adding an optInStatus parameter to the phone body.

# Done!

<!-- node@103 -->

If the response has a 204 status code, the new optInStatus will be added to your contact and associated with your canvass response, however it may take up to 30 minutes to be reflected to users.