Skip to main content
Required ACL: editSettings

Usage

// Initialize the client
var client = new AbtestingClient(
  new AbtestingConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", "ALGOLIA_APPLICATION_REGION")
);

// Call the API
var response = await client.AddABTestsAsync(
  new AddABTestsRequest
  {
    EndAt = "2022-12-31T00:00:00.000Z",
    Name = "myABTest",
    Variants = new List<AddABTestsVariant>
    {
      new AddABTestsVariant(new AbTestsVariant { Index = "AB_TEST_1", TrafficPercentage = 30 }),
      new AddABTestsVariant(new AbTestsVariant { Index = "AB_TEST_2", TrafficPercentage = 50 }),
    },
  }
);

// print the response
Console.WriteLine(response);
// Initialize the client with your application region, eg. abtesting.ALGOLIA_APPLICATION_REGION
client, err := abtesting.NewClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", abtesting.US)
if err != nil {
  // The client can fail to initialize if you pass an invalid parameter.
  panic(err)
}

// Call the API
response, err := client.AddABTests(client.NewApiAddABTestsRequest(

  abtesting.NewEmptyAddABTestsRequest().SetEndAt("2022-12-31T00:00:00.000Z").SetName("myABTest").SetVariants(
    []abtesting.AddABTestsVariant{*abtesting.AbTestsVariantAsAddABTestsVariant(
      abtesting.NewEmptyAbTestsVariant().SetIndex("AB_TEST_1").SetTrafficPercentage(30)), *abtesting.AbTestsVariantAsAddABTestsVariant(
      abtesting.NewEmptyAbTestsVariant().SetIndex("AB_TEST_2").SetTrafficPercentage(50))})))
if err != nil {
  // handle the eventual error
  panic(err)
}


// print the response
print(response)
// Initialize the client
AbtestingClient client = new AbtestingClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", "ALGOLIA_APPLICATION_REGION");

// Call the API
ABTestResponse response = client.addABTests(
  new AddABTestsRequest()
    .setEndAt("2022-12-31T00:00:00.000Z")
    .setName("myABTest")
    .setVariants(
      Arrays.asList(
        new AbTestsVariant().setIndex("AB_TEST_1").setTrafficPercentage(30),
        new AbTestsVariant().setIndex("AB_TEST_2").setTrafficPercentage(50)
      )
    )
);

// print the response
System.out.println(response);
// Initialize the client
// Replace 'us' with your Algolia Application Region
const client = algoliasearch('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY').initAbtesting({ region: 'us' });

// Call the API
const response = await client.addABTests({
  endAt: '2022-12-31T00:00:00.000Z',
  name: 'myABTest',
  variants: [
    { index: 'AB_TEST_1', trafficPercentage: 30 },
    { index: 'AB_TEST_2', trafficPercentage: 50 },
  ],
});


// print the response
console.log(response);
// Initialize the client
val client =
  AbtestingClient(
    appId = "ALGOLIA_APPLICATION_ID",
    apiKey = "ALGOLIA_API_KEY",
    region = "ALGOLIA_APPLICATION_REGION",
  )

// Call the API
var response =
  client.addABTests(
    addABTestsRequest =
      AddABTestsRequest(
        endAt = "2022-12-31T00:00:00.000Z",
        name = "myABTest",
        variants =
          listOf(
            AbTestsVariant(index = "AB_TEST_1", trafficPercentage = 30),
            AbTestsVariant(index = "AB_TEST_2", trafficPercentage = 50),
          ),
      )
  )


// print the response
println(response)
// Initialize the client
$client = AbtestingClient::create('ALGOLIA_APPLICATION_ID', 'ALGOLIA_API_KEY', 'ALGOLIA_APPLICATION_REGION');

// Call the API
$response = $client->addABTests(
    ['endAt' => '2022-12-31T00:00:00.000Z',
        'name' => 'myABTest',
        'variants' => [
            ['index' => 'AB_TEST_1',
                'trafficPercentage' => 30,
            ],

            ['index' => 'AB_TEST_2',
                'trafficPercentage' => 50,
            ],
        ],
    ],
);


// print the response
var_dump($response);
# Initialize the client
# In an asynchronous context, you can use AbtestingClient instead, which exposes the exact same methods.
client = AbtestingClientSync(
    "ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", "ALGOLIA_APPLICATION_REGION"
)

# Call the API
response = client.add_ab_tests(
    add_ab_tests_request={
        "endAt": "2022-12-31T00:00:00.000Z",
        "name": "myABTest",
        "variants": [
            {
                "index": "AB_TEST_1",
                "trafficPercentage": 30,
            },
            {
                "index": "AB_TEST_2",
                "trafficPercentage": 50,
            },
        ],
    },
)


# print the response
print(response)
# Initialize the client
client = Algolia::AbtestingClient.create("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY", "ALGOLIA_APPLICATION_REGION")

# Call the API
response = client.add_ab_tests(
  Algolia::Abtesting::AddABTestsRequest.new(
    end_at: "2022-12-31T00:00:00.000Z",
    name: "myABTest",
    variants: [
      Algolia::Abtesting::AbTestsVariant.new(index: "AB_TEST_1", traffic_percentage: 30),
      Algolia::Abtesting::AbTestsVariant.new(index: "AB_TEST_2", traffic_percentage: 50)
    ]
  )
)


# print the response
puts(response)
// Initialize the client
val client = AbtestingClient(
  appId = "ALGOLIA_APPLICATION_ID",
  apiKey = "ALGOLIA_API_KEY",
  region = Option("ALGOLIA_APPLICATION_REGION")
)

// Call the API
val response = Await.result(
  client.addABTests(
    addABTestsRequest = AddABTestsRequest(
      endAt = "2022-12-31T00:00:00.000Z",
      name = "myABTest",
      variants = Seq(
        AbTestsVariant(
          index = "AB_TEST_1",
          trafficPercentage = 30
        ),
        AbTestsVariant(
          index = "AB_TEST_2",
          trafficPercentage = 50
        )
      )
    )
  ),
  Duration(100, "sec")
)

// print the response
println(response)
// Initialize the client
let client = try AbtestingClient(appID: "ALGOLIA_APPLICATION_ID", apiKey: "ALGOLIA_API_KEY", region: .us)

// Call the API
let response = try await client.addABTests(addABTestsRequest: AbtestingAddABTestsRequest(
    name: "myABTest",
    variants: [
        AbtestingAddABTestsVariant.abtestingAbTestsVariant(AbtestingAbTestsVariant(
            index: "AB_TEST_1",
            trafficPercentage: 30
        )),
        AbtestingAddABTestsVariant.abtestingAbTestsVariant(AbtestingAbTestsVariant(
            index: "AB_TEST_2",
            trafficPercentage: 50
        )),
    ],
    endAt: "2022-12-31T00:00:00.000Z"
))

// print the response
print(response)

See the full API reference

For more details about input parameters and response fields.
Last modified on March 25, 2026