# Basic Usage

## Core Functionality

### Authenticate

The first step is to initialize the SDK client with the credentials that have been provided to you:

{% hint style="info" %}
**Note:** If you are both a **Search: Pex Registry** and **Search: Custom Database** customer, each product will require a separate set of credentials.
{% endhint %}

{% tabs %}
{% tab title="Python" %}
{% code fullWidth="false" %}

```python
# AUTHENTICATE CLIENT
client = pex.PexSearchClient("CLIENT_ID", "CLIENT_SECRET")
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}

```go
// AUTHENTICATE CLIENT
client, err := pex.NewPexSearchClient("clientID", "clientSecret")
if err != nil {
	panic(err)
}
defer client.Close()
```

{% endtab %}

{% tab title="PHP" %}

```php
// AUTHENTICATE CLIENT
$client = new Pex\PexSearchClient(CLIENT_ID, CLIENT_SECRET);
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// AUTHENTICATE CLIENT
let client = new pex.PexSearchClient(CLIENT_ID, CLIENT_SECRET)
await client.connect()
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** There might be a slight delay the first time you authenticate as the SDK downloads and installs necessary updates
{% endhint %}

### Generate Fingerprint

Before performing any searches you need to generate a fingerprint from a media file:

{% tabs %}
{% tab title="Python" %}

```python
# CREATE AN AUDIO FINGERPRINT FROM MEDIA FILE
ft = client.fingerprint_file(
    "/path/to/file.mp3",
    pex.FingerprintType.AUDIO
)

# CREATE A MELODY + PHONETIC FINGERPRINT FROM MEDIA FILE
ft = client.fingerprint_file(
    "/path/to/file.mp3",
    pex.FingerprintType.MELODY | pex.FingerprintType.PHONETIC
)

# CREATE A CLASSIFICATION FINGERPRINT FROM MEDIA FILE
ft = client.fingerprint_file(
    "/path/to/file.mp3",
    pex.FingerprintType.CLASSIFICATION
)
```

{% endtab %}

{% tab title="Go" %}

```go
// CREATE AN AUDIO FINGERPRINT FROM MEDIA FILE
ft, err := client.FingerprintFile(
    inputFile,
    pex.FingerprintTypeAudio,
)

// CREATE A MELODY + PHONETIC FINGERPRINT FROM MEDIA FILE
ft, err := client.FingerprintFile(
    inputFile,
    pex.FingerprintTypeMelody|pex.FingerprintTypePhonetic,
)

// CREATE A CLASSIFICATION FINGERPRINT FROM MEDIA FILE
ft, err := client.FingerprintFile(
    inputFile,
    pex.FingerprintTypeClassification,
)
```

{% endtab %}

{% tab title="PHP" %}

```php
// CREATE AN AUDIO FINGERPRINT FROM MEDIA FILE
$ft = $client->fingerprintFile(
    INPUT_FILE,
    [Pex\FingerprintType::Audio]
);

// CREATE A MELODY + PHONETIC FINGERPRINT FROM MEDIA FILE
$ft = $client->fingerprintFile(
    INPUT_FILE,
    [Pex\FingerprintType::Melody, Pex\FingerprintType::Phonetic]
);

// CREATE A CLASSIFICATION FINGERPRINT FROM MEDIA FILE
$ft = $client->fingerprintFile(
    INPUT_FILE,
    [Pex\FingerprintType::Classification]
);
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// CREATE AN AUDIO FINGERPRINT FROM MEDIA FILE
let ft = await client.fingerprintFile(
  "video.mp4",
  [pex.AUDIO]
);

// CREATE A MELODY + PHONETIC FINGERPRINT FROM MEDIA FILE
let ft = await client.fingerprintFile(
  "video.mp4",
  [pex.MELODY, pex.PHONETIC]
);

// CREATE A CLASSIFICATION FINGERPRINT FROM MEDIA FILE
let ft = await client.fingerprintFile(
  "video.mp4",
  [pex.CLASSIFICATION]
);
```

{% endtab %}
{% endtabs %}

Alternatively, a fingeprint can be created from a byte buffer holding a media file

{% tabs %}
{% tab title="Python" %}

```python
# CREATE AN AUDIO FINGERPRINT FROM BYTE BUFFER HOLDING A MEDIA FILE
ft = client.fingerprint_buffer([]byte, pex.FingerprintType.AUDIO)
```

{% endtab %}

{% tab title="Go" %}

```go
// CREATE AUDIO FINGERPRINT FROM BYTE BUFFER HOLDING A MEDIA FILE
buf := []byte{ /* media bytes */ }
ft, err = client.FingerprintBuffer(buf, pex.FingerprintTypeAudio)
if err != nil {
    panic(err)
}
```

{% endtab %}

{% tab title="PHP" %}

```php
// CREATE AUDIO FINGERPRINT FROM BYTE BUFFER HOLDING A MEDIA FILE
$buffer = /* binary audio data */;
$ft = $client->fingerprintBuffer(
    $buffer,
    [Pex\FingerprintType::Audio]
);
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// CREATE AUDIO FINGERPRINT FROM BYTE BUFFER HOLDING A MEDIA FILE
const buffer = Buffer.from(/* binary data here */);
let ft = await client.fingerprintBuffer(
  buffer,
  [pex.AUDIO]
);
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** When creating audio fingerprints, the SDK uses a single CPU. For melody fingerprints, the SDK uses two CPUs, if available.

If you need to process larger volumes of files, running computations in parallel is recommended. This can significantly improve processing capacity.
{% endhint %}

## Search Functionality

### Initiate Search (With Fingerprint)

Once the fingerprint has been generated, you are ready to initiate a search:

{% tabs %}
{% tab title="Python" %}

```python
# BUILD REQUEST FOR "IDENTIFY MUSIC" SEARCH TYPE
req = pex.PexSearchRequest(fingerprint=ft, type=pex.PexSearchType.IDENTIFY_MUSIC)

# BUILD REQUEST FOR "FIND MATCHES" SEARCH TYPE
req = pex.PexSearchRequest(fingerprint=ft, type=pex.PexSearchType.FIND_MATCHES)

# START SEARCH
future = client.start_search(req)
```

{% endtab %}

{% tab title="Go" %}

```go
// BUILD REQUEST FOR "IDENTIFY MUSIC" SEARCH TYPE
req := &pex.PexSearchRequest{
	Fingerprint: ft,
	Type:        pex.IdentifyMusic,
}

// BUILD REQUEST FOR "FIND MATCHES" SEARCH TYPE
req := &pex.PexSearchRequest{
	Fingerprint: ft,
	Type:        pex.FindMatches,
}

// START SEARCH
fut, err := client.StartSearch(req)
if err != nil {
	panic(err)
}
```

{% endtab %}

{% tab title="PHP" %}

```php
// BUILD REQUEST FOR "IDENTIFY MUSIC" SEARCH TYPE
$req = new Pex\PexSearchRequest($ft, Pex\PexSearchType::IdentifyMusic);

// BUILD REQUEST FOR "FIND MATCHES" SEARCH TYPE
$req = new Pex\PexSearchRequest($ft, Pex\PexSearchType::FindMatches);

// START SEARCH
$fut = $client->startSearch($req);
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// START SEARCH FOR "IDENTIFY MUSIC" SEARCH TYPE
let result = await client.startSearch({
  "fingerprint": ft,
  "type": pex.IDENTIFY_MUSIC,
})

// START SEARCH FOR "FIND MATCHES" SEARCH TYPE
let result = await client.startSearch({
  "fingerprint": ft,
  "type": pex.FIND_MATCHES,
})
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
A search combines a **fingerprint type** (how your file is analyzed) with a **search type** (what kind of lookup is performed). The combination you choose determines the results you’ll see. Refer to [search-types-and-use-cases](https://docs.pex.com/search/faq/search-types-and-use-cases "mention") for examples of common use cases and suggested combinations.
{% endhint %}

### Initiate Search (With ISRC)

If you know the ISRC of the query file you would like to search with, you can prepare a search with the ISRC instead of a fingerprint. If a corresponding asset is found associated with the ISRC, the search will initiate as usual.

{% tabs %}
{% tab title="Python" %}

```python
# BUILD THE ISRC SEARCH REQUEST
req = pex.ISRCSearchRequest(
    isrc="12345",
    ft_types=
        pex.FingerprintType.AUDIO,
    type=pex.PexSearchType.FIND_MATCHES,
)

# START SEARCH
future = client.start_isrc_search(req)
```

{% endtab %}

{% tab title="Go" %}

```go
// BUILD THE ISRC SEARCH REQUEST
req := &pex.ISRCSearchRequest{
    ISRC: "12345",
    FingerprintTypes: []pex.FingerprintType{
        pex.FingerprintTypeAudio,
    },
    Type: pex.FindMatches,
}

// START SEARCH
fut, err := client.StartISRCSearch(req)
if err != nil {
    panic(err)
}
```

{% endtab %}

{% tab title="PHP" %}

```php
// BUILD THE ISRC SEARCH REQUEST
$req = new Pex\ISRCSearchRequest(
    "12345",
    [
        Pex\FingerprintType::Audio,
    ],
    Pex\PexSearchType::FindMatches
);

// START SEARCH
$fut = $client->startISRCSearch($req);
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// START SEARCH
let result = await client.startISRCSearch({
  isrc: "12345",
  fingerprintTypes: [
    pex.AUDIO,
  ],
  type: pex.FIND_MATCHES,
});
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
If a corresponding asset cannot be resolved with the provided ISRC, the SDK returns a NOT\_FOUND error (for example: `NOT_FOUND: isrc not found`). In this case, the request is rejected and no search is started. Your application should catch/handle this error and decide whether to prompt for a different ISRC, skip the lookup request, or log/report the failure
{% endhint %}

### Retrieve Search Results

Once a search is complete, you can retrieve the results of the search:

{% tabs %}
{% tab title="Python" %}

```python
# RETRIEVE SEARCH RESULTS
result = future.get()
```

{% endtab %}

{% tab title="Go" %}

```go
// RETRIEVE SEARCH RESULTS
res, err := fut.Get()
if err != nil {
	panic(err)
}
```

{% endtab %}

{% tab title="PHP" %}

```php
// RETRIEVE SEARCH RESULTS
$res = $fut->get();
```

{% endtab %}

{% tab title="Node.js" %}

```javascript
// THE STARTSEARCH FUNCTION DESCRIBED IN THE PREVIOUS SECTION WILL AUTOMATICALLY RETRIEVE THE RESULTS WHEN THE SEARCH IS COMPLETED
```

{% endtab %}
{% endtabs %}

### Interpret Search Results

To view details on what's contained in a search response, please see the following section: [search-response](https://docs.pex.com/search/sdk-integration/search-pex-registry/search-response "mention")
