curl -X POST 'https://api.growthbook.io/api/v1/segments' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"name":"Annual Subscribers","datasourceId":"ds_123abc","identifierType":"user_id","type":"SQL","query":"SELECT plan FROM subscribers WHERE plan = "}'import requests
url = "https://api.growthbook.io/api/v1/segments"
payload = {
"name": "<string>",
"datasourceId": "<string>",
"identifierType": "<string>",
"owner": "<string>",
"description": "<string>",
"projects": ["<string>"],
"query": "<string>",
"factTableId": "<string>",
"filters": ["<string>"]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: '<string>',
datasourceId: '<string>',
identifierType: '<string>',
owner: '<string>',
description: '<string>',
projects: ['<string>'],
query: '<string>',
factTableId: '<string>',
filters: ['<string>']
})
};
fetch('https://api.growthbook.io/api/v1/segments', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.growthbook.io/api/v1/segments",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => '<string>',
'datasourceId' => '<string>',
'identifierType' => '<string>',
'owner' => '<string>',
'description' => '<string>',
'projects' => [
'<string>'
],
'query' => '<string>',
'factTableId' => '<string>',
'filters' => [
'<string>'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.growthbook.io/api/v1/segments"
payload := strings.NewReader("{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.growthbook.io/api/v1/segments")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.growthbook.io/api/v1/segments")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}"
response = http.request(request)
puts response.read_body{
"segment": {
"id": "<string>",
"owner": "<string>",
"datasourceId": "<string>",
"identifierType": "<string>",
"name": "<string>",
"dateCreated": "<string>",
"dateUpdated": "<string>",
"ownerEmail": "<string>",
"description": "<string>",
"query": "<string>",
"factTableId": "<string>",
"filters": [
"<string>"
],
"projects": [
"<string>"
]
}
}Authorizations
If using Bearer auth, pass the Secret Key as the token:
curl https://api.growthbook.io/api/v1/features -H "Authorization: Bearer secret_abc123DEF456"
Body
Name of the segment
ID of the datasource this segment belongs to
Type of identifier (user, anonymous, etc.)
GrowthBook supports two types of Segments, SQL and FACT. SQL segments are defined by a SQL query, and FACT segments are defined by a fact table and filters.
SQL, FACT The userId or email address of the owner. If an email address is provided, it will be used to look up the userId of the matching organization member. If an ID is provided, it will be validated as existing in the organization.
Description of the segment
10000List of project IDs for projects that can access this segment
Where this Segment must be managed from. If not set (empty string), it can be managed from anywhere.
, api SQL query that defines the Segment. This is required for SQL segments.
ID of the fact table this segment belongs to. This is required for FACT segments.
Optional array of fact table filter ids that can further define the Fact Table based Segment.
Response
Resource created
Show child attributes
Show child attributes
Was this page helpful?
curl -X POST 'https://api.growthbook.io/api/v1/segments' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"name":"Annual Subscribers","datasourceId":"ds_123abc","identifierType":"user_id","type":"SQL","query":"SELECT plan FROM subscribers WHERE plan = "}'import requests
url = "https://api.growthbook.io/api/v1/segments"
payload = {
"name": "<string>",
"datasourceId": "<string>",
"identifierType": "<string>",
"owner": "<string>",
"description": "<string>",
"projects": ["<string>"],
"query": "<string>",
"factTableId": "<string>",
"filters": ["<string>"]
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
name: '<string>',
datasourceId: '<string>',
identifierType: '<string>',
owner: '<string>',
description: '<string>',
projects: ['<string>'],
query: '<string>',
factTableId: '<string>',
filters: ['<string>']
})
};
fetch('https://api.growthbook.io/api/v1/segments', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.growthbook.io/api/v1/segments",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'name' => '<string>',
'datasourceId' => '<string>',
'identifierType' => '<string>',
'owner' => '<string>',
'description' => '<string>',
'projects' => [
'<string>'
],
'query' => '<string>',
'factTableId' => '<string>',
'filters' => [
'<string>'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.growthbook.io/api/v1/segments"
payload := strings.NewReader("{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.growthbook.io/api/v1/segments")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.growthbook.io/api/v1/segments")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"name\": \"<string>\",\n \"datasourceId\": \"<string>\",\n \"identifierType\": \"<string>\",\n \"owner\": \"<string>\",\n \"description\": \"<string>\",\n \"projects\": [\n \"<string>\"\n ],\n \"query\": \"<string>\",\n \"factTableId\": \"<string>\",\n \"filters\": [\n \"<string>\"\n ]\n}"
response = http.request(request)
puts response.read_body{
"segment": {
"id": "<string>",
"owner": "<string>",
"datasourceId": "<string>",
"identifierType": "<string>",
"name": "<string>",
"dateCreated": "<string>",
"dateUpdated": "<string>",
"ownerEmail": "<string>",
"description": "<string>",
"query": "<string>",
"factTableId": "<string>",
"filters": [
"<string>"
],
"projects": [
"<string>"
]
}
}
