All Products
Search
Document Center

OpenSearch:C# request construction example

Last Updated:Mar 18, 2026

This topic describes how to use the C# SDK to construct requests in OpenSearch.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.

  • For information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.

    2. Restart Windows for the AccessKey pair to take effect.

Install package dependencies

Package dependency URL: https://www.nuget.org/packages

dotnet add package AlibabaCloud.TeaUtil --version  0.1.5
dotnet add package AlibabaCloud.OpenSearchUtil --version 1.0.2
dotnet add package Aliyun.Credentials --version  1.2.1
dotnet add package Tea --version 0.4.0

Note:

When you download the dependencies, make sure that you download the specified versions. If an error similar to the following occurs, download the dependency versions specified in this topic again.

System.MissingMethodException: Method not found: 'System.String Tea.Utils.Extensions.Get(System.Collections.Generic.Dictionary`2<System.String,System.String>, System.String, System.String)'.
   at AlibabaCloud.OpenSearchUtil.Common.GetSignature(TeaRequest request, String accessKeySecret)
   at AlibabaCloud.OpenSearchUtil.Common.GetSignature(TeaRequest request, String accessKeyId, String accessKeySecret)
   at Client._request(String method, String pathname, Dictionary`2 query, Dictionary`2 headers, Object body, RuntimeOptions runtime) in D:\workspace\C#_workspace\MyDoNet\Client.cs:line 131
   at MyDoNet.Program.searchDoc(Client opensearchClient, String appName, Dictionary`2 queryParams, Dictionary`2 header, RuntimeOptions runTime) in D:\workspace\C#_workspace\MyDoNet\Program.cs:line 17
   at MyDoNet.Program.Main(String[] args) in D:\workspace\C#_workspace\MyDoNet\Program.cs:line 84

Sample code

The following code provides an example of how to construct a request:

// This file is auto-generated, don't edit it. Thanks.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;


public class Client
{
  protected string _endpoint;
  protected string _protocol;
  protected string _userAgent;
  protected Aliyun.Credentials.Client _credential;

  public Client(Config config)
  {
    if (AlibabaCloud.TeaUtil.Common.IsUnset(config.ToMap()))
    {
      throw new TeaException(new Dictionary<string, string>
      {
        {"name", "ParameterMissing"},
        {"message", "'config' can not be unset"},
      });
    }

    if (AlibabaCloud.TeaUtil.Common.Empty(config.Type))
    {
      config.Type = "access_key";
    }

    Aliyun.Credentials.Models.Config credentialConfig = new Aliyun.Credentials.Models.Config
    {
      AccessKeyId = config.AccessKeyId,
      Type = config.Type,
      AccessKeySecret = config.AccessKeySecret,
      SecurityToken = config.SecurityToken,
    };
    this._credential = new Aliyun.Credentials.Client(credentialConfig);
    this._endpoint = config.Endpoint;
    this._protocol = config.Protocol;
    this._userAgent = config.UserAgent;
  }

  public Dictionary<string, object> _request(string method, string pathname, Dictionary<string, object> query,
    Dictionary<string, string> headers, object body, AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime)
  {
    Dictionary<string, object> runtime_ = new Dictionary<string, object>
    {
      {"timeouted", "retry"},
      {"readTimeout", runtime.ReadTimeout},
      {"connectTimeout", runtime.ConnectTimeout},
      {"httpProxy", runtime.HttpProxy},
      {"httpsProxy", runtime.HttpsProxy},
      {"noProxy", runtime.NoProxy},
      {"maxIdleConns", runtime.MaxIdleConns},
      {"retry", new Dictionary<string, object>
        {
          {"retryable", runtime.Autoretry},
          {"maxAttempts", AlibabaCloud.TeaUtil.Common.DefaultNumber(runtime.MaxAttempts, 1)},
        }
      },
      {"backoff", new Dictionary<string, object>
        {
          {"policy", AlibabaCloud.TeaUtil.Common.DefaultString(runtime.BackoffPolicy, "no")},
          {"period", AlibabaCloud.TeaUtil.Common.DefaultNumber(runtime.BackoffPeriod, 1)},
        }
      },
      {"ignoreSSL", runtime.IgnoreSSL},
    };

    TeaRequest _lastRequest = null;
    Exception _lastException = null;
    long _now = System.DateTime.Now.Millisecond;
    int _retryTimes = 0;
    while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
    {
      if (_retryTimes > 0)
      {
        int backoffTime = TeaCore.GetBackoffTime((IDictionary) runtime_["backoff"], _retryTimes);
        if (backoffTime > 0)
        {
          TeaCore.Sleep(backoffTime);
        }
      }

      _retryTimes = _retryTimes + 1;
      try

      {
        TeaRequest request_ = new TeaRequest();
        // User identity information
        // Read the AccessKey ID and AccessKey secret from the environment variables.
        // Before you run the sample code, you must configure the environment variables. For more information, see the "Configure environment variables" section in this topic.
        string accesskeyId = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
        string accessKeySecret = System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        string securityToken = this._credential.GetSecurityToken();
        request_.Protocol = AlibabaCloud.TeaUtil.Common.DefaultString(_protocol, "HTTP");
        request_.Method = method;
        request_.Pathname = pathname;
        request_.Headers = TeaConverter.merge<string>
        (
          new Dictionary<string, string>()
          {
            {"user-agent",  AlibabaCloud.TeaUtil.Common.GetUserAgent(this._userAgent )},
            {"Date", AlibabaCloud.OpenSearchUtil.Common.GetDate()},
            {"host", AlibabaCloud.TeaUtil.Common.DefaultString(_endpoint, "opensearch-cn-hangzhou.aliyuncs.com")},
            {"X-Opensearch-Nonce", AlibabaCloud.TeaUtil.Common.GetNonce()},
          },
          headers
        );
        if (!AlibabaCloud.TeaUtil.Common.IsUnset(query))
        {
          request_.Query = AlibabaCloud.TeaUtil.Common.StringifyMapValue(query);
        }

        if (!AlibabaCloud.TeaUtil.Common.IsUnset(body))
        {
          string reqBody = AlibabaCloud.TeaUtil.Common.ToJSONString(body);
          request_.Headers["Content-MD5"] = AlibabaCloud.OpenSearchUtil.Common.GetContentMD5(reqBody);
          request_.Headers["Content-Type"] = "application/json";
          request_.Body = TeaCore.BytesReadable(reqBody);
        }

        if (!AlibabaCloud.TeaUtil.Common.IsUnset(securityToken))
        {
          request_.Headers["X-Opensearch-Security-Token"] = securityToken;
        }

        request_.Headers["Authorization"] =
          AlibabaCloud.OpenSearchUtil.Common.GetSignature(request_, accesskeyId, accessKeySecret);
        _lastRequest = request_;
        TeaResponse response_ = TeaCore.DoAction(request_, runtime_);

        string objStr = AlibabaCloud.TeaUtil.Common.ReadAsString(response_.Body);
        object obj = AlibabaCloud.TeaUtil.Common.ParseJSON(objStr);
        Console.WriteLine(objStr);
        Dictionary<string, object> res = AlibabaCloud.TeaUtil.Common.AssertAsMap(obj);

        if (AlibabaCloud.TeaUtil.Common.Is4xx(response_.StatusCode) ||
            AlibabaCloud.TeaUtil.Common.Is5xx(response_.StatusCode))
        {
          throw new TeaException(new Dictionary<string, object>
          {
            {"message", response_.StatusMessage},
            {"data", res},
            {"code", response_.StatusCode},
          });
        }

        return new Dictionary<string, object>
        {
          {"body", res},
          {"headers", response_.Headers}
        };
      }
      catch (Exception e)
      {
        if (TeaCore.IsRetryable(e))
        {
          _lastException = e;
          continue;
        }
        throw;
      }
    }
    throw new TeaUnretryableException(_lastRequest, _lastException);
  }
}