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

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.

Be sure to read the next API Design article: Best Practices for Building a Secure and Scalable API

Original Article

Using field masks with Google APIs for partial response