Set user agent
Stay organized with collections
Save and categorize content based on your preferences.
Set a custom user agent on a BigQuery client.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to set a custom user agent for a BigQuery client using both Java and Node.js.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Java code sample initializes a \u003ccode\u003eHeaderProvider\u003c/code\u003e with a custom user agent value, which is then used when building the \u003ccode\u003eBigQuery\u003c/code\u003e client.\u003c/p\u003e\n"],["\u003cp\u003eThe Node.js example shows setting a user agent directly within the \u003ccode\u003eBigQuery\u003c/code\u003e client initialization using a simple option.\u003c/p\u003e\n"],["\u003cp\u003eBoth examples utilize the user agent values for tracking purposes, and require authentication setup with Application Default Credentials, as detailed in the documentation links.\u003c/p\u003e\n"]]],[],null,["# Set user agent\n\nSet a custom user agent on a BigQuery client.\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.FixedHeaderProvider.html;\n import com.google.api.gax.rpc.https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.HeaderProvider.html;\n import com.google.auth.oauth2.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.common.collect.ImmutableMap;\n import java.io.IOException;\n\n public class SetUserAgent {\n\n private static final String USER_AGENT_HEADER = \"user-agent\";\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"my-project-id\";\n String customUserAgentValue = \"my-custom-user-agent-value\";\n setUserAgent(projectId, customUserAgentValue);\n }\n\n public static void setUserAgent(String projectId, String customUserAgentValue)\n throws IOException {\n // Setup the credentials\n https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html googleCredentials = https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html.https://cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials.html#com_google_auth_oauth2_GoogleCredentials_getApplicationDefault__();\n\n // Initialize the HeaderProvider object with custom user agent value\n https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.HeaderProvider.html headerProvider =\n https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.FixedHeaderProvider.html.create(ImmutableMap.of(USER_AGENT_HEADER, customUserAgentValue));\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigQuery =\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.newBuilder()\n .setProjectId(projectId)\n .https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ClientContext.Builder.html#com_google_api_gax_rpc_ClientContext_Builder_setCredentials_com_google_auth_Credentials_(googleCredentials)\n .setHeaderProvider(headerProvider)\n .build()\n .getService();\n\n System.out.println(bigQuery.getOptions().getUserAgent());\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Node.js API\nreference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n // Import the Google Cloud client library\n const {BigQuery} = require('https://cloud.google.com/nodejs/docs/reference/bigquery/latest/overview.html');\n // Create a client and set the user agent\n const bigquery = new https://cloud.google.com/nodejs/docs/reference/bigquery/latest/bigquery/bigquery.html({userAgent: 'my-user-agent'});\n\n console.log('User agent:');\n console.log(bigquery.providedUserAgent);\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]