A segment is a named, project-scoped set of attribute-equality conditions, defined once and referenced by id from any flag's targeting rules. This tutorial defines a segment for enterprise-plan customers, points a rule at it, and evaluates against it.
1. Create the segment
POST /v1/projects/{project_id}/segments takes 1-20 AND-composed conditions:
const res = await fetch(
`https://api.rollfuse.com/v1/projects/${projectId}/segments`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Enterprise plan customers",
conditions: [{ attribute: "plan", value: "enterprise" }],
}),
}
);
const segment = await res.json();
console.log(segment.id, segment.name);
Every condition is an equality check on an attribute your evaluation calls pass in (here, plan). Because the segment is a standalone resource, updating it later with PATCH /v1/segments/{segment_id} changes the behavior of every rule that references it, in every flag, without touching those flags individually.
2. Reference the segment from a targeting rule
Targeting rules live on a flag's per-environment config, set with PUT /v1/environments/{environment_id}/feature-flags/{feature_flag_id}/config. A rule's outcome points at a variation id, and its condition is either an inline attribute/value pair or a segment_id, never both:
const enterpriseVariationId = flag.variations.find((v) => v.key === "on").id;
const defaultVariationId = flag.variations.find((v) => v.key === "off").id;
const configRes = await fetch(
`https://api.rollfuse.com/v1/environments/${environmentId}/feature-flags/${flag.id}/config`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
enabled: true,
default_variation_id: defaultVariationId,
rules: [
{
segment_id: segment.id,
outcome: { variation_id: enterpriseVariationId },
},
],
}),
}
);
Rules are evaluated in order, and any subject that doesn't match a rule falls through to default_variation_id. Because this config already carries a manually-authored rule, you can't also attach a percentage rollout to it: rollouts and manual rules are mutually exclusive on the same environment config.
3. Confirm targeting from the SDK
Evaluate with the same attribute the segment checks. A subject with plan: "enterprise" matches the segment and gets the rule's outcome; anyone else falls through to the default:
import { RollfuseClient } from "@rollfuse/sdk-js";
const client = new RollfuseClient({
baseUrl: "https://api.rollfuse.com",
credential: process.env.ROLLFUSE_CREDENTIAL!, // set before running
});
await client.start();
const enterpriseResult = client.evaluate("user-42", "new-checkout-flow", {
attributes: { plan: "enterprise" },
fallback: false,
});
console.log(enterpriseResult.value, enterpriseResult.reason); // matched the segment rule
const otherResult = client.evaluate("user-99", "new-checkout-flow", {
attributes: { plan: "starter" },
fallback: false,
});
console.log(otherResult.value, otherResult.reason); // fell through to default_variation_id
Where to go next
Segments compose the same condition set across every flag that references them; if instead you want to gradually expand exposure to everyone rather than a fixed group, see the percentage rollout tutorial.