Skip to content

Commit c49a6da

Browse files
tested and updated all requests
1 parent 4e41bac commit c49a6da

7 files changed

+72
-45
lines changed

readme.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ We'll walk through some of the most common operations in the lifecycle of an exp
88

99
To get started with this API cookbook, first complete the below setup.
1010

11+
### Create Optimizely Account
12+
13+
If you're not an existing enterprise customer, create a completely free feature flagging account with Optimizely [here](https://www.optimizely.com/free-feature-flagging/) (no credit card required!).
14+
1115
### Install VS Code + Extensions
1216

13-
We'll be using VS Code as a REST client make requests to the Optimizely APIs. Use the below links to install:
17+
We'll be using VS Code as a REST client make requests to the Optimizely APIs. Use the below links to install your IDE (integrated development environment) and needed extensions:
1418

1519
- [VS Code](https://code.visualstudio.com/download)
16-
- [Rest Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) (REST Client for VS Code)
20+
- [Rest Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) (REST Client extension for VS Code)
1721

1822
### Get Optimizely API Personal Access Token
1923

@@ -23,17 +27,17 @@ Follow [these steps](https://docs.developers.optimizely.com/feature-experimentat
2327

2428
Next, we'll setup VS Code so we can make requests to the Optimizely APIs.
2529

26-
Let's create some environment variables for the Optimizely API endpoints and your personal access token.
30+
Let's create some environment variables for the Optimizely API endpoints and your personal access token that we can reuse across API requests.
2731

28-
In VS Code, create a directory named `.vscode` in the root of your project. In this directory, create a file named `settings.json`. Make sure to replace the `token` value with your personal access token:
32+
In VS Code, create a directory named `.vscode` in the root of your project. In this directory, create a file named `settings.json` and copy and paste the below into the file. Make sure to replace `{{token}}` with your personal access token and `{{yourProjectId}}` with your project ID (you can find this in the URL from the app homepage, e.g. `https://app.optimizely.com/v2/projects/{{your ProjectId}}/flags/list`):
2933

3034
```
3135
{
3236
"rest-client.environmentVariables": {
3337
"$shared": {
3438
"flagsUrl": "https://api.optimizely.com/flags/v1",
3539
"baseUrl": "https://api.optimizely.com/v2",
36-
"token": "{{yourToken}}",
40+
"token": "Bearer {{yourToken}}",
3741
"projectId": {{yourProjectId}}
3842
}
3943
}
@@ -56,6 +60,25 @@ Now we'll execute some of the most common operations in the lifecycle of an expe
5660
8. Conclude the experiment.
5761
9. Analyze results.
5862

59-
Each of the above sections has its own `.rest` file in the `requests` directory of this repo. To execute the requests, open each file in VS Code and click the `Send Request` link above the request.
63+
Each of the above sections has its own `.rest` file in the `requests` directory of this repo. At this point, the directory structure of your project should be as follows:
64+
65+
```
66+
📦fx-api-recipes
67+
┣ 📂.vscode
68+
┃ ┗ 📜settings.json
69+
┣ 📂requests
70+
┃ ┣ 📜1_create_flag.rest
71+
┃ ┣ 📜2_create_variations.rest
72+
┃ ┣ 📜3_create_events.rest
73+
┃ ┣ 📜4_create_attributes.rest
74+
┃ ┣ 📜5_create_audiences.rest
75+
┃ ┣ 📜6_create_experiment.rest
76+
┃ ┣ 📜7_launch_experiment.rest
77+
┃ ┣ 📜8_conclude_experiment.rest
78+
┃ ┗ 📜9_analyze_results.rest
79+
┗ 📜readme.md
80+
```
81+
82+
To execute the requests, open each `.rest` file in VS Code and click the `Send Request` link above the request.
6083

6184
For more information on how to send requests using the REST Client extension, see [here](https://marketplace.visualstudio.com/items?itemName=humao.rest-client#usage).

requests/4_create_attributes.rest

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,65 @@ Authorization: {{token}}
88

99
{
1010
"key": "is_logged_in",
11-
"project_id": 26592390574
11+
"project_id": {{projectId}} // Do not wrap in quotes, must be integer value. VS Code may highlight this as an error, but it is not.
1212
}
1313
###
1414

1515
POST {{baseUrl}}/attributes
1616
Content-Type: application/json
1717
Authorization: {{token}}
18+
1819
{
1920
"key": "days_since_last_order",
20-
"project_id": 26592390574
21+
"project_id": {{projectId}}
2122
}
2223
###
2324

2425
POST {{baseUrl}}/attributes
2526
Content-Type: application/json
2627
Authorization: {{token}}
28+
2729
{
2830
"key": "region_state",
29-
"project_id": 26592390574
31+
"project_id": {{projectId}}
3032
}
3133
###
3234

3335
POST {{baseUrl}}/attributes
3436
Content-Type: application/json
3537
Authorization: {{token}}
38+
3639
{
3740
"key": "is_mobile_user",
38-
"project_id": 26592390574
41+
"project_id": {{projectId}}
3942
}
4043
###
4144

4245
POST {{baseUrl}}/attributes
4346
Content-Type: application/json
4447
Authorization: {{token}}
48+
4549
{
4650
"key": "is_mobile_app",
47-
"project_id": 26592390574
51+
"project_id": {{projectId}}
4852
}
4953
###
5054

5155
POST {{baseUrl}}/attributes
5256
Content-Type: application/json
5357
Authorization: {{token}}
58+
5459
{
5560
"key": "is_qa",
56-
"project_id": 26592390574
61+
"project_id": {{projectId}}
5762
}
5863
###
5964

6065
POST {{baseUrl}}/attributes
6166
Content-Type: application/json
6267
Authorization: {{token}}
68+
6369
{
6470
"key": "qa_team",
65-
"project_id": 26592390574
71+
"project_id": {{projectId}}
6672
}

requests/5_create_audiences.rest

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
POST {{baseUrl}}/audiences
66
Content-Type: application/json
77
Authorization: {{token}}
8+
// Do not wrap {{projectId}} in quotes, must be integer value. VS Code may highlight this as an error, but it is not.
89

910
{
1011
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"is_logged_in\",\"value\":false,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]",
1112
"name": "Anonymous Users",
12-
"project_id": 26592390574
13+
"project_id": {{projectId}}
1314
}
15+
1416
###
1517
POST {{baseUrl}}/audiences
1618
Content-Type: application/json
@@ -19,7 +21,7 @@ Authorization: {{token}}
1921
{
2022
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"is_logged_in\",\"value\":true,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]",
2123
"name": "Authenticated Users",
22-
"project_id": 26592390574
24+
"project_id": {{projectId}}
2325
}
2426
###
2527
POST {{baseUrl}}/audiences
@@ -29,7 +31,7 @@ Authorization: {{token}}
2931
{
3032
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"days_since_last_order\",\"type\":\"custom_attribute\",\"match_type\":\"le\",\"value\":14}]]]",
3133
"name": "Recent Purchasers",
32-
"project_id": 26592390574
34+
"project_id": {{projectId}}
3335
}
3436
###
3537
POST {{baseUrl}}/audiences
@@ -39,7 +41,7 @@ Authorization: {{token}}
3941
{
4042
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"region_state\",\"type\":\"custom_attribute\",\"value\":\"CA\"}]]]",
4143
"name": "California Visitors",
42-
"project_id": 26592390574
44+
"project_id": {{projectId}}
4345
}
4446
###
4547
POST {{baseUrl}}/audiences
@@ -49,7 +51,7 @@ Authorization: {{token}}
4951
{
5052
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"is_mobile_user\",\"value\":true,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]",
5153
"name": "Mobile Web Users",
52-
"project_id": 26592390574
54+
"project_id": {{projectId}}
5355
}
5456
###
5557
POST {{baseUrl}}/audiences
@@ -59,7 +61,7 @@ Authorization: {{token}}
5961
{
6062
"conditions": "[\"and\",[\"or\",[\"or\",{\"name\":\"is_mobile_app\",\"value\":true,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]",
6163
"name": "Mobile App Users",
62-
"project_id": 26592390574
64+
"project_id": {{projectId}}
6365
}
6466
###
6567
POST {{baseUrl}}/audiences
@@ -69,7 +71,7 @@ Authorization: {{token}}
6971
{
7072
"conditions": "[\"and\",[\"or\",[\"or\", {\"value\": true,\"type\": \"custom_attribute\",\"name\": \"is_qa\"}]],[\"or\",[\"or\",{\"name\":\"qa_team\",\"type\":\"custom_attribute\",\"value\":\"1\"}]]]",
7173
"name": "QA Team 1",
72-
"project_id": 26592390574
74+
"project_id": {{projectId}}
7375
}
7476
###
7577
POST {{baseUrl}}/audiences
@@ -79,5 +81,5 @@ Authorization: {{token}}
7981
{
8082
"conditions": "[\"and\",[\"or\",[\"or\", {\"value\": true,\"type\": \"custom_attribute\",\"name\": \"is_qa\"}]],[\"or\",[\"or\",{\"name\":\"qa_team\",\"type\":\"custom_attribute\",\"value\":\"2\"}]]]",
8183
"name": "QA Team 2",
82-
"project_id": 26592390574
84+
"project_id": {{projectId}}
8385
}

requests/6_create_experiment.rest

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
// Distribution Mode: manual
1111
// Variations: off (33%), On Show Amounts (33%), On Show Amounts Red (33%)
1212

13-
//TODO: Fix url
14-
PATCH {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/production/ruleset
13+
PATCH {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/{{environment}}/ruleset
1514
Content-Type: application/json
1615
Authorization: {{token}}
1716

@@ -29,7 +28,7 @@ Authorization: {{token}}
2928
{
3029
"aggregator": "unique",
3130
"display_title": "add_to_cart",
32-
"event_id": 25259131694,
31+
"event_id": 26574151708,
3332
"event_type": "custom",
3433
"scope": "visitor",
3534
"winning_direction": "increasing"
@@ -56,14 +55,9 @@ Authorization: {{token}}
5655
}
5756
},
5857
{
59-
"op": "replace",
58+
"op": "add",
6059
"path": "/rule_priorities/0",
6160
"value": "stock_message_test"
62-
},
63-
{
64-
"op": "add",
65-
"path": "/rule_priorities/1",
66-
"value": "td"
6761
}
6862
]
6963

requests/7_launch_experiment.rest

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
// ------------------------------- Enable Flag ------------------------------ //
66

7-
POST https://api.app.optimizely.com/flags/projects/25276451226/flags/inventory_on_pdp/environments/production/ruleset/enabled
7+
POST {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/{{environment}}/ruleset/enabled
88
Authorization: {{token}}
99

10-
11-
1210
###
1311

1412
// ---------------------------- Launch Experiment --------------------------- //
1513

16-
//TODO: test url
17-
PATCH {{flagsUrl}}/projects/25276451226/flags/inventory_on_pdp/environments/production/ruleset
14+
PATCH {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/{{environment}}/ruleset
1815
Content-Type: application/json
1916
Authorization: {{token}}
2017

requests/8_conclude_experiment.rest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Conclude experiment. #
33
# ---------------------------------------------------------------------------- #
44

5-
PATCH {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/production/ruleset
5+
PATCH {{flagsUrl}}/projects/{{projectId}}/flags/{{flagKey}}/environments/{{environment}}/ruleset
66
Content-Type: application/json
77
Authorization: {{token}}
88

requests/9_analyze_results.rest

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@
44

55
// ----------- Get All Experiments in Project => Get Experiment Key ---------- //
66

7-
GET {{flagsUrl}}/projects/{{projectId}}/rules?rule_types=a/b,mab&archived=false&environments=production&per_page=100
7+
GET {{flagsUrl}}/projects/{{projectId}}/rules?rule_types=a/b,mab&archived=false&environments={{environment}}&per_page=100
88
Content-Type: application/json
99
Authorization: {{token}}
1010

1111
// Grab the relevant experiment key from the response, if you don't already have it (e.g., stock_message_test).
1212

1313
###
1414

15-
// ------------------------- Get Experiment Reports ------------------------- //
16-
GET {{flagsUrl}}/projects/{{projectId}}/environments/production/reports?sort=key:asc&archived=false
15+
// ------------------------- Get Experiment Reports => Get Report Key ------------------------- //
16+
GET {{flagsUrl}}/projects/{{projectId}}/environments/{{environment}}/reports?sort=key:asc&archived=false
1717
Content-Type: application/json
1818
Authorization: {{token}}
1919

20-
// Grab the relevant report key for the experiment of interest (e.g., stock_message_test_W9PHyDfYDiGdoqwuU9ANR4)
20+
// Grab the relevant report key for the experiment of interest (e.g., stock_message_test_ff8g3hxNc8EKoE837vVVtR)
2121

2222
###
2323

24-
// -------------------- Get Individual Experiment Report -------------------- //
25-
GET {{flagsUrl}}/projects/{{projectId}}/environments/production/reports/{yourReportKey}
24+
// -------------------- Get Individual Experiment Report => Get Experiment Results ID -------------------- //
25+
26+
// Replace {{reportKey}} with the report key from the previous response.
27+
28+
GET {{flagsUrl}}/projects/{{projectId}}/environments/{{environment}}/reports/{{reportKey}}
2629
Content-Type: application/json
2730
Authorization: {{token}}
2831

29-
// Grab the relevant experiment results ID from the end of the fetch_results_ui_url key in the response
32+
// Grab the relevant experiment results ID from the end of the fetch_results_ui_url key in the response (/experiments/{{experimentResultsId}})
33+
34+
###
3035

3136
// ------------------------- Get Experiment Results ------------------------- //
3237

33-
// Add the experiment results from the previous response ID to the URL below.
38+
// Replace {{experimentResultsId}} with the experiment results ID from the previous response.
3439

35-
GET {{baseUrl}}/experiments/{yourExperimentResultsId}/results
40+
GET {{baseUrl}}/experiments/{{experimentResultsId}}/results
3641
Content-Type: application/json
3742
Authorization: {{token}}

0 commit comments

Comments
 (0)