You are here

How to Use Field Masks With Google APIs For Filtered Responses

Often API responses give you way more data than you ever wanted. Normally this isn’t a problem but sometimes you’d really rather receive just the fields you want in the payload to avoid putting strain on your app. Wesley Chun at the G Suite Developers blog shows you how to do exactly that with Google APIs and field masks.

Field masks are simply masks you add to API calls to limit what fields the API will return. Apart from the savings you make in terms of data processing, Wesley emphasizes that field masks can make API responses faster too, since the server knows to fetch only the data you’re interested in.

To show you how it’s done, Wesley gives the example of using the GMAIL Python client library to make a call to fetch your sender addresses without any extraneous data.

You could have done just the same with the RESTful API and an endpoint like this: GET https://www.googleapis.com/gmail/v1/users/userId/settings/sendAs

Either of these calls will return the following. 
 

This gives you everything you need for each of your sender addresses. This is probably too many fields for you. You can cut all those fields to just get the email signature by specifying a field mask on the SendAs attribute with SendAsEmail and perhaps the isPrimary flag. This will cut out all the other fields in the response. 

In another tutorial, Wesley plans to show you how to use fields masks for update API calls. You can learn more about how to use field masks with Google client libraries here.

Original Article

Using field masks with Google APIs for partial response

Seamus Holland
 

Comments