# Create your segment

To specify the segment of contacts you want to target with your campaign, you need to select the set of requirements to be fulfilled by those contacts. This is done via the `Expression` property in a `contactfilter` object.

### Example

cURL
```shell
curl -s \
	-X POST \
	--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
	https://api.mailjet.com/v3/REST/contactfilter \
	-H 'Content-Type: application/json' \
	-d '{
      "Description":"Will send only to contacts under 35 years of age.",
      "Expression":"(age<35)",
      "Name":"Customers under 35"
	}'
```

PHP
```php
<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'),true,['version' => 'v3']);
$body = [
  'Description' => "Will send only to contacts under 35 years of age.",
  'Expression' => "(age<35)",
  'Name' => "Customers under 35"
];
$response = $mj->post(Resources::$Contactfilter, ['body' => $body]);
$response->success() && var_dump($response->getData());
?>
```

Node.js
```javascript
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.post("contactfilter", {'version': 'v3'})
	.request({
      "Description":"Will send only to contacts under 35 years of age.",
      "Expression":"(age<35)",
      "Name":"Customers under 35"
    })
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})
```

Ruby
```ruby
require 'mailjet'
Mailjet.configure do |config|
  config.api_key = ENV['MJ_APIKEY_PUBLIC']
  config.secret_key = ENV['MJ_APIKEY_PRIVATE']
  config.api_version = "v3"
end
variable = Mailjet::Contactfilter.create(
  description: "Will send only to contacts under 35 years of age.",
  expression: "(age<35)",
  name: "Customers under 35"
)
p variable.attributes['Data']
```

Python
```python
from mailjet_rest import Client
import os
api_key = os.environ['MJ_APIKEY_PUBLIC']
api_secret = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(api_key, api_secret), version='v3')
data = {
  'Description': "Will send only to contacts under 35 years of age.",
  'Expression': "(age<35)",
  'Name': "Customers under 35"
}
result = mailjet.contactfilter.create(data=data)
print result.status_code
print result.json()
```

Java
```java
package com.my.project;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.errors.MailjetSocketTimeoutException;
import com.mailjet.client.MailjetClient;
import com.mailjet.client.MailjetRequest;
import com.mailjet.client.MailjetResponse;
import com.mailjet.client.ClientOptions;
import com.mailjet.client.resource.Contactfilter;
import org.json.JSONArray;
import org.json.JSONObject;
public class MyClass {
    public static void main(String[] args) throws MailjetException, MailjetSocketTimeoutException {
      MailjetClient client;
      MailjetRequest request;
      MailjetResponse response;
      client = new MailjetClient(System.getenv("MJ_APIKEY_PUBLIC"), System.getenv("MJ_APIKEY_PRIVATE"), new ClientOptions("v3"));
      request = new MailjetRequest(Contactfilter.resource)
            .property(Contactfilter.DESCRIPTION, "Will send only to contacts under 35 years of age.")
            .property(Contactfilter.EXPRESSION, "(age<35)")
            .property(Contactfilter.NAME, "Customers under 35")
      );
      response = client.post(request);
      System.out.println(response.getStatus());
      System.out.println(response.getData());
    }
}
```

Go
```go
package main
import (
	"fmt"
	"log"
	"os"
	mailjet "github.com/mailjet/mailjet-apiv3-go"
	"github.com/mailjet/mailjet-apiv3-go/resources"
)
func main () {
	mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
	var data []resources.Contactfilter
	mr := &Request{
	  Resource: "contactfilter",
	}
	fmr := &FullRequest{
	  Info: mr,
	  Payload: &resources.Contactfilter { 
	  Description:"Will send only to contacts under 35 years of age.",
	  Expression:"(age<35)",
	  Name:"Customers under 35"
      }
	}
	err := mailjetClient.Post(fmr, &data)
	if err != nil {
	  fmt.Println(err)
	}
	fmt.Printf("Data array: %+v\n", data)
}
```

C#
```csharp
using Mailjet.Client;
using Mailjet.Client.Resources;
using System;
using Newtonsoft.Json.Linq;
namespace Mailjet.ConsoleApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         RunAsync().Wait();
      }
      static async Task RunAsync()
      {
         MailjetClient client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE"))
         {
            Version = ApiVersion.V3,
         };
         MailjetRequest request = new MailjetRequest
         {
            Resource = Contactfilter.Resource,
         }
             .Property(Contactfilter.Description, "Will send only to contacts under 35 years of age.")
             .Property(Contactfilter.Expression, "(age<35)")
             .Property(Contactfilter.Name, "Customers under 35");
         MailjetResponse response = await client.PostAsync(request);
         if (response.IsSuccessStatusCode)
         {
            Console.WriteLine(string.Format("Total: {0}, Count: {1}\n", response.GetTotal(), response.GetCount()));
            Console.WriteLine(response.GetData());
         }
         else
         {
            Console.WriteLine(string.Format("StatusCode: {0}\n", response.StatusCode));
            Console.WriteLine(string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()));
            Console.WriteLine(response.GetData());
            Console.WriteLine(string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
         }
      }
   }
}
```

### API Response

```
{
	"Count": 1,
	"Data": [
		{
			"Description": "Will send only to contacts under 35 years of age.",
			"Expression": "(age<35)",
			"Name": "Customers under 35",
			"ID": 123,
			"Status": "unused"
		}
	],
	"Total": 1
}
```

### Simple expressions

You can create simple expressions using the `=`, `<`, `>` and `!=` operators. Every single expression should be presented in parentheses.

`"Expression": "(string_property=\"male\")"`

`"Expression": "(integer_property<40)"`

`"Expression": "(boolean_property=true)"`

You can also apply the negative statement not. When using not, the expression should be surrounded by an additional set of parentheses:

`"Expression": "((not IsProvided(date)))"`

### Complex expressions

You can combine simple expressions using AND and OR operators to formulate complex ones. When entering more than two expressions, you need to specify the order of operations by adding additional sets of parentheses.

`"Expression": "((string_property=\"male\") AND (integer_property<32)) OR (boolean_property=true)"`

`"Expression": "(string_property=\"male\") AND ((integer_property<32) OR (boolean_property=true))"`