> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Importing with the API

> How to index your data to Algolia using an API client.

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

Algolia doesn't directly search your data sources.
Instead, you upload the parts of your data that are relevant for search to Algolia.
Algolia stores this data in an <Index />:
a data structure optimized for fast search.

* For more information about preparing your data for indexing, see [Format and structure your data](/doc/guides/sending-and-managing-data/prepare-your-data).
* For alternatives to the API for pushing data, see [Send your data to Algolia](/doc/guides/sending-and-managing-data/send-and-update-your-data).

## Required credentials

To send data to Algolia, you need an Application ID and a valid API key (with [`addObjects`](/doc/guides/security/api-keys#access-control-list-acl) permission).
You can find them in the **API Keys** section of [Algolia's dashboard](https://dashboard.algolia.com/account/api-keys/all).

* **Application ID**. Your Application ID is what Algolia uses to identify your app, where all your indices live.
* **API key**. [API keys](/doc/guides/security/api-keys) control access to Algolia's API and determine what you're allowed to do, such as searching an index, or adding new <Records />. For better security, create specific API keys with minimal [permissions](/doc/guides/security/api-keys#access-control-list-acl) for indexing tasks, which you should only use in server-side code. Keep your indexing API keys secret.

**Only use the [Admin API key](/doc/guides/security/api-keys#admin-api-key) to create other API keys**.
Don't use the Admin API key in your apps.

## Fetch your data

Before sending anything to Algolia, **you need to retrieve your data**.
You can do this in several ways, depending on the nature of your app.

### From a database

<CodeGroup>
  ```cs C# theme={"system"}
  IEnumerable FetchDataFromDataBase() {
    // Fetch data from your database
  }

  var actors = FetchDataFromDataBase();
  ```

  ```go Go theme={"system"}
  func fetchDataFromDatabase() []Actor {
  	// Fetch data from your database
  }

  func main() {
  	records := fetchDataFromDatabase()
  }
  ```

  ```java Java theme={"system"}
  public List fetchDataFromDatabase() {
    // Fetch data from your database
  }

  List actors = fetchDataFromDataBase();
  ```

  ```js JavaScript theme={"system"}
  const fetchDataFromDatabase = () => {
  	// Fetch data from your database.
  };

  const records = fetchDataFromDatabase();
  ```

  ```kt Kotlin theme={"system"}
  fun fetchFromDatabase(): List {
    // Fetch data from your database
  }

  val actors: List = fetchFromDatabase()
  ```

  ```php PHP theme={"system"}
  function fetchDataFromDatabase() {
    // Fetch data from your database
  }

  $records = fetchDataFromDatabase();
  ```

  ```python Python theme={"system"}
  def fetch_data_from_database():
    data = {}
    # Fetch data from your database
    return data

  records = fetch_data_from_database()
  ```

  ```ruby Ruby theme={"system"}
  def fetch_data_from_database
    # Fetch data from your database
  end

  records = fetch_data_from_database
  ```

  ```scala Scala theme={"system"}
  def fetchDataFromDatabase(): Iterable[Actor] = {
    // Fetch data from your database
  }

  val actors = fetchDataFromDatabase()
  ```

  ```swift Swift theme={"system"}
  func fetchDataFromDataBase() -> [[String: Any]] {
    // Fetch data from your database
  }

  let records = fetchDataFromDataBase()
  ```
</CodeGroup>

### From a file

You can use this [actors dataset](https://github.com/algolia/datasets/blob/master/movies/actors.json) to test this out.

<CodeGroup>
  ```cs C# theme={"system"}
  using System.Collections.Generic;
  using System.IO;
  using Algolia.Search.Clients;
  using Newtonsoft.Json;
  using Newtonsoft.Json.Serialization;

  public class Actor
  {
      public string Name { get; set; }
      public string ObjectID { get; set; }
      public int Rating { get; set; }
      public string ImagePath { get; set; }
      public string AlternativePath { get; set; }
  }

  public class AlgoliaActorsIndexer
  {
      private SearchClient client;
      private SearchIndex index;

      public AlgoliaActorsIndexer(string appId, string apiKey, string indexName)
      {
          client = new SearchClient(appId, apiKey);
          index = client.InitIndex(indexName);
      }

      public void IndexActorsFromFile(string filePath)
      {
          var settings = new JsonSerializerSettings
          {
              ContractResolver = new DefaultContractResolver
              {
                  NamingStrategy = new CamelCaseNamingStrategy()
              }
          };

          IEnumerable actors = JsonConvert.DeserializeObject>(File.ReadAllText(filePath), settings);
          index.SaveObjects(actors);
      }
  }

  // Usage
  public class Program
  {
      public static void Main(string[] args)
      {
          AlgoliaActorsIndexer indexer = new AlgoliaActorsIndexer("undefined", "undefined", "actors");
          indexer.IndexActorsFromFile("actors.json");
      }
  }
  ```

  ```go Go theme={"system"}
  package main

  import (
  	"encoding/json"
  	"io/ioutil"
  )

  type Actor struct {
  	Name            string `json:"name"`
  	Rating          int    `json:"rating"`
  	ImagePath       string `json:"image_path"`
  	AlternativeName string `json:"alternative_name"`
  	ObjectID        string `json:"objectID"`
  }

  func main() {
  	var actors []Actor
  	data, _ := ioutil.ReadFile("actors.json")
  	_ = json.Unmarshal(data, &actors)
  }
  ```

  ```java Java theme={"system"}
  import java.io.FileInputStream;
  import java.io.InputStream;
  import com.fasterxml.jackson.databind.ObjectMapper;

  public class Actor {
      // Getters/Setters ommitted
      private String name;
      private String objectID;
      private int rating;
      private String imagePath;
      private String alternativePath;
  }

  ObjectMapper objectMapper = Defaults.getObjectMapper();

  InputStream input = new FileInputStream("actors.json");
  Actor[] actors = objectMapper.readValue(input, Actor[].class);
  ```

  ```js JavaScript theme={"system"}
  const records = require("./actors.json");
  ```

  ```kt Kotlin theme={"system"}
  @Serializable
  data class Actor(
      val name: String,
      val rating: Int,
      val imagePath: String,
      val alternativePath: String,
      override val objectID: ObjectID
  ) : Indexable

  val string = File("actors.json").readText()
  val actors: List = Json.plain.parse(Actor.serializer().list, string)
  ```

  ```php PHP theme={"system"}
  $records = json_decode(file_get_contents('actors.json'), true);
  ```

  ```python Python theme={"system"}
  import json

  with open('actors.json') as f:
      records = json.load(f)
  ```

  ```ruby Ruby theme={"system"}
  require 'json'

  file    = File.read('actors.json')
  records = JSON.parse(file)
  ```

  ```scala Scala theme={"system"}
  import org.json4s._
  import org.json4s.native.JsonMethods._

  case class Actor(name: String,
                   rating: Int,
                   image_path: String,
                   alternative_path: Option[String],
                   objectID: String)

  object Main {

    def main(args: Array[String]): Unit = {

      val json = parse(new FileInputStream("actors.json")).extract[Seq[Actor]]

    }

  }
  ```

  ```swift Swift theme={"system"}
  let filePath = Bundle.main.path(forResource: "actors", ofType: "json")!
  let contentData = FileManager.default.contents(atPath: filePath)!
  let records = try! JSONSerialization.jsonObject(with: contentData, options: []) as! [[String: Any]]
  ```
</CodeGroup>

### From the source code directly

Only use this method for exploration purposes or if you have a small amount of data.

<CodeGroup>
  ```cs C# theme={"system"}
  using System.Collections.Generic;

  public class Actor
  {
      public string Name { get; set; }
  }

  public class Program
  {
      public static void Main()
      {
          IEnumerable records = new List
          {
              new Actor { Name = "Tom Cruise" },
              new Actor { Name = "Scarlett Johansson" }
          };
      }
  }
  ```

  ```go Go theme={"system"}
  actors := []Actor{
  	{Name: "Tom Cruise"},
  	{Name: "Scarlett Johansson"},
  }
  ```

  ```java Java theme={"system"}
  public class Person {

     private String name;

     public Person() {}

     public String getName() {
       return name;
     }

     public Person setName(String name) {
       this.name = name;
       return this;
     }
   }

   ArrayList persons = new ArrayList() {{
      add(new Person().setName("Tom Cruise"));
      add(new Person().setName("Scarlett Johansson"));
   }};
  ```

  ```js JavaScript theme={"system"}
  const records = [{ name: "Tom Cruise" }, { name: "Scarlett Johansson" }];
  ```

  ```kt Kotlin theme={"system"}
  val records = listOf(json { "name" to "Tom Cruise" }, json { "name" to "Scarlett Johansson" })
  ```

  ```php PHP theme={"system"}
  $records = [
    ['name' => 'Tom Cruise'],
    ['name' => 'Scarlett Johansson']
  ];
  ```

  ```python Python theme={"system"}
  records = [
      {'name': 'Tom Cruise'},
      {'name': 'Scarlett Johansson'}
  ]
  ```

  ```ruby Ruby theme={"system"}
  records = [
    { name: 'Tom Cruise' },
    { name: 'Scarlett Johansson' }
  ]
  ```

  ```scala Scala theme={"system"}
  case class Person(name: String)

  val records = Seq(
    Person("Tom Cruise"),
    Person("Scarlett Johansson"),
  )
  ```

  ```swift Swift theme={"system"}
  let records: [[String: Any]] = [
    ["name": "Tom Cruise"],
    ["name": "Scarlett Johansson"],
  ]
  ```
</CodeGroup>

## Send the data to Algolia

Once the records are ready, you can push them to Algolia using the [`saveObjects`](/doc/libraries/sdk/v1/methods/save-objects) method.

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SaveObjectsAsync(
    "INDEX_NAME",
    new List<Object>
    {
      new Dictionary<string, string> { { "objectID", "1" }, { "name", "Adam" } },
      new Dictionary<string, string> { { "objectID", "2" }, { "name", "Benoit" } },
    }
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SaveObjects(
    "INDEX_NAME",
    []map[string]any{{"objectID": "1", "name": "Adam"}, {"objectID": "2", "name": "Benoit"}})
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  List response = client.saveObjects(
    "INDEX_NAME",
    Arrays.asList(
      new HashMap() {
        {
          put("objectID", "1");
          put("name", "Adam");
        }
      },
      new HashMap() {
        {
          put("objectID", "2");
          put("name", "Benoit");
        }
      }
    )
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.saveObjects({
    indexName: 'cts_e2e_saveObjects_javascript',
    objects: [
      { objectID: '1', name: 'Adam' },
      { objectID: '2', name: 'Benoit' },
    ],
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.saveObjects(
      indexName = "INDEX_NAME",
      objects =
        listOf(
          buildJsonObject {
            put("objectID", JsonPrimitive("1"))
            put("name", JsonPrimitive("Adam"))
          },
          buildJsonObject {
            put("objectID", JsonPrimitive("2"))
            put("name", JsonPrimitive("Benoit"))
          },
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->saveObjects(
      'INDEX_NAME',
      [
          ['objectID' => '1',
              'name' => 'Adam',
          ],

          ['objectID' => '2',
              'name' => 'Benoit',
          ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.save_objects(
      index_name="INDEX_NAME",
      objects=[
          {
              "objectID": "1",
              "name": "Adam",
          },
          {
              "objectID": "2",
              "name": "Benoit",
          },
      ],
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.save_objects(
    "INDEX_NAME",
    [{objectID: "1", name: "Adam"}, {objectID: "2", name: "Benoit"}]
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.saveObjects(
      indexName = "INDEX_NAME",
      objects = Seq(
        JObject(List(JField("objectID", JString("1")), JField("name", JString("Adam")))),
        JObject(List(JField("objectID", JString("2")), JField("name", JString("Benoit"))))
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.saveObjects(
      indexName: "INDEX_NAME",
      objects: [["objectID": "1", "name": "Adam"], ["objectID": "2", "name": "Benoit"]]
  )
  ```
</CodeGroup>

### Send your data in batches

For better performance, you should send several records at once instead of one by one.
If you have many records to index,
you should [send them in batches](/doc/guides/sending-and-managing-data/send-and-update-your-data/how-to/sending-records-in-batches).

## Next steps

After indexing your data, you can [configure relevance settings](/doc/guides/managing-results/relevance-overview).
