-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenapi.json
1833 lines (1833 loc) · 89.9 KB
/
openapi.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"openapi": "3.0.3",
"info": {
"termsOfService": "https://platform.stability.ai/docs/terms-of-service",
"description": "Welcome to the Stability.ai REST API!\n\nYour DreamStudio API key will be required for authentication: [How to find your API key](https://platform.stability.ai/docs/getting-started/authentication)\n\nAPI operations use the following versioning scheme:\n- `/v*` interface is stable and ready for production workloads\n- `/v*beta*`: interface is stable, preparing for production release\n- `/v*alpha*`: under development and the interface is subject to change\n\nNOTE: The v1alpha and v1beta endpoints from the developer preview are still available, but they\nwill disabled on May 1st, 2023. Please migrate to the v1 endpoints as soon as possible.\n\nIf you have feedback or encounter any issues with the API, please reach out:\n - [https://github.com/Stability-AI/REST-API](https://github.com/Stability-AI/REST-API)\n - [https://discord.gg/stablediffusion #API channel](https://discord.com/channels/1002292111942635562/1042896447311454361)\n",
"title": "Stability.ai REST API",
"version": "v1",
"x-logo": {
"altText": "Stability.ai REST API",
"url": "/docs/StabilityLogo.png"
}
},
"servers": [
{
"url": "https://api.stability.ai"
}
],
"tags": [
{
"name": "v1/user",
"description": "Manage your Stability.ai account, and view account/organization balances"
},
{
"name": "v1/engines",
"description": "Enumerate available engines"
},
{
"name": "v1/generation",
"description": "Generate images from text, existing images, or both"
}
],
"paths": {
"/v1/generation/{engine_id}/text-to-image": {
"post": {
"description": "Generate a new image from a text prompt",
"operationId": "textToImage",
"summary": "text-to-image",
"tags": [
"v1/generation"
],
"parameters": [
{
"$ref": "#/components/parameters/engineID"
},
{
"$ref": "#/components/parameters/accept"
},
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"requestBody": {
"content": {
"application/json": {
"example": {
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"height": 512,
"width": 512,
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 1,
"steps": 75,
"text_prompts": [
{
"text": "A lighthouse on a cliff",
"weight": 1
}
]
},
"schema": {
"$ref": "#/components/schemas/TextToImageRequestBody"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK response.",
"content": {
"application/json": {
"schema": {
"description": "The result of the generation request, containing one or more images as base64 encoded strings.",
"items": {
"$ref": "#/components/schemas/Image"
},
"type": "array"
}
},
"image/png": {
"example": "PNG bytes, what did you expect?",
"schema": {
"description": "The bytes of the generated image",
"format": "binary",
"type": "string"
}
}
},
"headers": {
"Content-Length": {
"$ref": "#/components/headers/Content-Length"
},
"Content-Type": {
"$ref": "#/components/headers/Content-Type"
},
"Finish-Reason": {
"$ref": "#/components/headers/Finish-Reason"
},
"Seed": {
"$ref": "#/components/headers/Seed"
}
}
},
"400": {
"$ref": "#/components/responses/400FromGeneration"
},
"401": {
"$ref": "#/components/responses/401"
},
"403": {
"$ref": "#/components/responses/403"
},
"404": {
"$ref": "#/components/responses/404"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import base64\nimport os\nimport requests\n\nengine_id = \"stable-diffusion-xl-1024-v1-0\"\napi_host = os.getenv('API_HOST', 'https://api.stability.ai')\napi_key = os.getenv(\"STABILITY_API_KEY\")\n\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.post(\n f\"{api_host}/v1/generation/{engine_id}/text-to-image\",\n headers={\n \"Content-Type\": \"application/json\",\n \"Accept\": \"application/json\",\n \"Authorization\": f\"Bearer {api_key}\"\n },\n json={\n \"text_prompts\": [\n {\n \"text\": \"A lighthouse on a cliff\"\n }\n ],\n \"cfg_scale\": 7,\n \"height\": 1024,\n \"width\": 1024,\n \"samples\": 1,\n \"steps\": 30,\n },\n)\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\ndata = response.json()\n\nfor i, image in enumerate(data[\"artifacts\"]):\n with open(f\"./out/v1_txt2img_{i}.png\", \"wb\") as f:\n f.write(base64.b64decode(image[\"base64\"]))\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\nimport fs from 'node:fs'\n\nconst engineId = 'stable-diffusion-xl-1024-v1-0'\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst apiKey = process.env.STABILITY_API_KEY\n\nif (!apiKey) throw new Error('Missing Stability API key.')\n\nconst response = await fetch(\n `${apiHost}/v1/generation/${engineId}/text-to-image`,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({\n text_prompts: [\n {\n text: 'A lighthouse on a cliff',\n },\n ],\n cfg_scale: 7,\n height: 1024,\n width: 1024,\n steps: 30,\n samples: 1,\n }),\n }\n)\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface GenerationResponse {\n artifacts: Array<{\n base64: string\n seed: number\n finishReason: string\n }>\n}\n\nconst responseJSON = (await response.json()) as GenerationResponse\n\nresponseJSON.artifacts.forEach((image, index) => {\n fs.writeFileSync(\n `./out/v1_txt2img_${index}.png`,\n Buffer.from(image.base64, 'base64')\n )\n})\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/base64\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"os\"\n)\n\ntype TextToImageImage struct {\n\tBase64 string `json:\"base64\"`\n\tSeed uint32 `json:\"seed\"`\n\tFinishReason string `json:\"finishReason\"`\n}\n\ntype TextToImageResponse struct {\n\tImages []TextToImageImage `json:\"artifacts\"`\n}\n\nfunc main() {\n\t// Build REST endpoint URL w/ specified engine\n\tengineId := \"stable-diffusion-xl-1024-v1-0\"\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/generation/\" + engineId + \"/text-to-image\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\tvar data = []byte(`{\n\t\t\"text_prompts\": [\n\t\t {\n\t\t\t\"text\": \"A lighthouse on a cliff\"\n\t\t }\n\t\t],\n\t\t\"cfg_scale\": 7,\n\t\t\"height\": 1024,\n\t\t\"width\": 1024,\n\t\t\"samples\": 1,\n\t\t\"steps\": 30\n \t}`)\n\n\treq, _ := http.NewRequest(\"POST\", reqUrl, bytes.NewBuffer(data))\n\treq.Header.Add(\"Content-Type\", \"application/json\")\n\treq.Header.Add(\"Accept\", \"application/json\")\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\n\t// Execute the request & read all the bytes of the body\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\n\tif res.StatusCode != 200 {\n\t\tvar body map[string]interface{}\n\t\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tpanic(fmt.Sprintf(\"Non-200 response: %s\", body))\n\t}\n\n\t// Decode the JSON body\n\tvar body TextToImageResponse\n\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Write the images to disk\n\tfor i, image := range body.Images {\n\t\toutFile := fmt.Sprintf(\"./out/v1_txt2img_%d.png\", i)\n\t\tfile, err := os.Create(outFile)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\timageBytes, err := base64.StdEncoding.DecodeString(image.Base64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif _, err := file.Write(imageBytes); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif err := file.Close(); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\nOUTPUT_FILE=./out/v1_txt2img.png\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image\"\n\ncurl -f -sS -X POST \"$URL\" \\\n -H 'Content-Type: application/json' \\\n -H 'Accept: image/png' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\" \\\n --data-raw '{\n \"text_prompts\": [\n {\n \"text\": \"A lighthouse on a cliff\"\n }\n ],\n \"cfg_scale\": 7,\n \"height\": 1024,\n \"width\": 1024,\n \"samples\": 1,\n \"steps\": 30\n }' \\\n -o \"$OUTPUT_FILE\"\n"
}
]
}
},
"/v1/generation/{engine_id}/image-to-image": {
"post": {
"description": "Modify an image based on a text prompt",
"operationId": "imageToImage",
"summary": "image-to-image",
"tags": [
"v1/generation"
],
"parameters": [
{
"$ref": "#/components/parameters/engineID"
},
{
"$ref": "#/components/parameters/accept"
},
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/ImageToImageRequestBody"
},
"examples": {
"IMAGE_STRENGTH": {
"summary": "Using IMAGE_STRENGTH",
"description": "Request using 35% image_strength",
"value": {
"image_strength": 0.35,
"init_image_mode": "IMAGE_STRENGTH",
"init_image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 3,
"steps": 20
}
},
"STEP_SCHEDULE": {
"summary": "Using STEP_SCHEDULE",
"description": "Equivalent request using step_schedule_start",
"value": {
"step_schedule_start": 0.65,
"init_image_mode": "STEP_SCHEDULE",
"init_image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 3,
"steps": 20
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK response.",
"content": {
"application/json": {
"schema": {
"description": "The result of the generation request, containing one or more images as base64 encoded strings.",
"items": {
"$ref": "#/components/schemas/Image"
},
"type": "array"
}
},
"image/png": {
"example": "PNG bytes, what did you expect?",
"schema": {
"description": "The bytes of the generated image",
"format": "binary",
"type": "string"
}
}
},
"headers": {
"Content-Length": {
"$ref": "#/components/headers/Content-Length"
},
"Content-Type": {
"$ref": "#/components/headers/Content-Type"
},
"Finish-Reason": {
"$ref": "#/components/headers/Finish-Reason"
},
"Seed": {
"$ref": "#/components/headers/Seed"
}
}
},
"400": {
"$ref": "#/components/responses/400FromGeneration"
},
"401": {
"$ref": "#/components/responses/401"
},
"403": {
"$ref": "#/components/responses/403"
},
"404": {
"$ref": "#/components/responses/404"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import base64\nimport os\nimport requests\n\nengine_id = \"stable-diffusion-xl-1024-v1-0\"\napi_host = os.getenv(\"API_HOST\", \"https://api.stability.ai\")\napi_key = os.getenv(\"STABILITY_API_KEY\")\n\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.post(\n f\"{api_host}/v1/generation/{engine_id}/image-to-image\",\n headers={\n \"Accept\": \"application/json\",\n \"Authorization\": f\"Bearer {api_key}\"\n },\n files={\n \"init_image\": open(\"../init_image_1024.png\", \"rb\")\n },\n data={\n \"image_strength\": 0.35,\n \"init_image_mode\": \"IMAGE_STRENGTH\",\n \"text_prompts[0][text]\": \"Galactic dog with a cape\",\n \"cfg_scale\": 7,\n \"samples\": 1,\n \"steps\": 30,\n }\n)\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\ndata = response.json()\n\nfor i, image in enumerate(data[\"artifacts\"]):\n with open(f\"./out/v1_img2img_{i}.png\", \"wb\") as f:\n f.write(base64.b64decode(image[\"base64\"]))\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\nimport FormData from 'form-data'\nimport fs from 'node:fs'\n\nconst engineId = 'stable-diffusion-xl-1024-v1-0'\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst apiKey = process.env.STABILITY_API_KEY\n\nif (!apiKey) throw new Error('Missing Stability API key.')\n\n// NOTE: This example is using a NodeJS FormData library.\n// Browsers should use their native FormData class.\n// React Native apps should also use their native FormData class.\nconst formData = new FormData()\nformData.append('init_image', fs.readFileSync('../init_image_1024.png'))\nformData.append('init_image_mode', 'IMAGE_STRENGTH')\nformData.append('image_strength', 0.35)\nformData.append('text_prompts[0][text]', 'Galactic dog wearing a cape')\nformData.append('cfg_scale', 7)\nformData.append('samples', 1)\nformData.append('steps', 30)\n\nconst response = await fetch(\n `${apiHost}/v1/generation/${engineId}/image-to-image`,\n {\n method: 'POST',\n headers: {\n ...formData.getHeaders(),\n Accept: 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: formData,\n }\n)\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface GenerationResponse {\n artifacts: Array<{\n base64: string\n seed: number\n finishReason: string\n }>\n}\n\nconst responseJSON = (await response.json()) as GenerationResponse\n\nresponseJSON.artifacts.forEach((image, index) => {\n fs.writeFileSync(\n `out/v1_img2img_${index}.png`,\n Buffer.from(image.base64, 'base64')\n )\n})\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/base64\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"mime/multipart\"\n\t\"net/http\"\n\t\"os\"\n)\n\ntype ImageToImageImage struct {\n\tBase64 string `json:\"base64\"`\n\tSeed uint32 `json:\"seed\"`\n\tFinishReason string `json:\"finishReason\"`\n}\n\ntype ImageToImageResponse struct {\n\tImages []ImageToImageImage `json:\"artifacts\"`\n}\n\nfunc main() {\n\tengineId := \"stable-diffusion-xl-1024-v1-0\"\n\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/generation/\" + engineId + \"/image-to-image\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\tdata := &bytes.Buffer{}\n\twriter := multipart.NewWriter(data)\n\n\t// Write the init image to the request\n\tinitImageWriter, _ := writer.CreateFormField(\"init_image\")\n\tinitImageFile, initImageErr := os.Open(\"../init_image_1024.png\")\n\tif initImageErr != nil {\n\t\tpanic(\"Could not open init_image.png\")\n\t}\n\t_, _ = io.Copy(initImageWriter, initImageFile)\n\n\t// Write the options to the request\n\t_ = writer.WriteField(\"init_image_mode\", \"IMAGE_STRENGTH\")\n\t_ = writer.WriteField(\"image_strength\", \"0.35\")\n\t_ = writer.WriteField(\"text_prompts[0][text]\", \"Galactic dog with a cape\")\n\t_ = writer.WriteField(\"cfg_scale\", \"7\")\n\t_ = writer.WriteField(\"samples\", \"1\")\n\t_ = writer.WriteField(\"steps\", \"30\")\n\twriter.Close()\n\n\t// Execute the request\n\tpayload := bytes.NewReader(data.Bytes())\n\treq, _ := http.NewRequest(\"POST\", reqUrl, payload)\n\treq.Header.Add(\"Content-Type\", writer.FormDataContentType())\n\treq.Header.Add(\"Accept\", \"application/json\")\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\n\tif res.StatusCode != 200 {\n\t\tvar body map[string]interface{}\n\t\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tpanic(fmt.Sprintf(\"Non-200 response: %s\", body))\n\t}\n\n\t// Decode the JSON body\n\tvar body ImageToImageResponse\n\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Write the images to disk\n\tfor i, image := range body.Images {\n\t\toutFile := fmt.Sprintf(\"./out/v1_img2img_%d.png\", i)\n\t\tfile, err := os.Create(outFile)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\timageBytes, err := base64.StdEncoding.DecodeString(image.Base64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif _, err := file.Write(imageBytes); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif err := file.Close(); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\nOUTPUT_FILE=./out/v1_img2img.png\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/generation/stable-diffusion-xl-1024-v1-0/image-to-image\"\n\ncurl -f -sS -X POST \"$URL\" \\\n -H 'Content-Type: multipart/form-data' \\\n -H 'Accept: image/png' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\" \\\n -F 'init_image=@\"../init_image_1024.png\"' \\\n -F 'init_image_mode=IMAGE_STRENGTH' \\\n -F 'image_strength=0.35' \\\n -F 'text_prompts[0][text]=A galactic dog in space' \\\n -F 'cfg_scale=7' \\\n -F 'samples=1' \\\n -F 'steps=30' \\\n -o \"$OUTPUT_FILE\"\n"
}
]
}
},
"/v1/generation/{engine_id}/image-to-image/upscale": {
"post": {
"description": "Create a higher resolution version of an input image.\n\nThis operation outputs an image with a maximum pixel count of **4,194,304**. This is equivalent to dimensions such as `2048x2048` and `4096x1024`.\n\nBy default, the input image will be upscaled by a factor of 2. For additional control over the output dimensions, a `width` or `height` parameter may be specified.\n\nFor upscaler engines that are ESRGAN-based, refer to the `RealESRGANUpscaleRequestBody` body option below. For upscaler engines that are Stable Diffusion Latent Upscaler-based, refer to the `LatentUpscalerUpscaleRequestBody` body option below.\n\nFor more details on the upscaler engines, refer to the [documentation on the Platform site.](https://platform.stability.ai/docs/features/image-upscaling?tab=python)\n",
"operationId": "upscaleImage",
"summary": "image-to-image/upscale",
"tags": [
"v1/generation"
],
"parameters": [
{
"$ref": "#/components/parameters/upscaleEngineID"
},
{
"$ref": "#/components/parameters/accept"
},
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/RealESRGANUpscaleRequestBody"
},
{
"$ref": "#/components/schemas/LatentUpscalerUpscaleRequestBody"
}
]
},
"examples": {
"ESRGAN": {
"description": "Upscale input image by 2x using ESRGAN.",
"value": {
"image": "<image binary>"
}
},
"DESIRED_WIDTH": {
"description": "Upscale input image to desired width with ESRGAN or the Latent Upscaler.",
"value": {
"image": "<image binary>",
"width": 1024
}
},
"DESIRED_HEIGHT": {
"description": "Upscale input image to desired height with ESRGAN or the Latent Upscaler.",
"value": {
"image": "<image binary>",
"height": 1024
}
},
"LATENT_UPSCALER": {
"description": "Request using the Latent Upscaler. Refer to the LatentUpscalerUpscaleRequestBody for reference.",
"value": {
"seed": 5555,
"steps": 20,
"image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK response.",
"content": {
"application/json": {
"schema": {
"description": "The result of the generation request, containing one or more images as base64 encoded strings.",
"items": {
"$ref": "#/components/schemas/Image"
},
"type": "array"
}
},
"image/png": {
"example": "PNG bytes, what did you expect?",
"schema": {
"description": "The bytes of the generated image",
"format": "binary",
"type": "string"
}
}
},
"headers": {
"Content-Length": {
"$ref": "#/components/headers/Content-Length"
},
"Content-Type": {
"$ref": "#/components/headers/Content-Type"
},
"Finish-Reason": {
"$ref": "#/components/headers/Finish-Reason"
},
"Seed": {
"$ref": "#/components/headers/Seed"
}
}
},
"400": {
"$ref": "#/components/responses/400FromUpscale"
},
"401": {
"$ref": "#/components/responses/401"
},
"403": {
"$ref": "#/components/responses/403"
},
"404": {
"$ref": "#/components/responses/404"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import os\nimport requests\n\nengine_id = \"esrgan-v1-x2plus\"\napi_host = os.getenv(\"API_HOST\", \"https://api.stability.ai\")\napi_key = os.getenv(\"STABILITY_API_KEY\")\n\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.post(\n f\"{api_host}/v1/generation/{engine_id}/image-to-image/upscale\",\n headers={\n \"Accept\": \"image/png\",\n \"Authorization\": f\"Bearer {api_key}\"\n },\n files={\n \"image\": open(\"../init_image.png\", \"rb\")\n },\n data={\n \"width\": 1024,\n }\n)\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\nwith open(f\"./out/v1_upscaled_image.png\", \"wb\") as f:\n f.write(response.content)\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\nimport FormData from 'form-data'\nimport fs from 'node:fs'\n\nconst engineId = 'esrgan-v1-x2plus'\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst apiKey = process.env.STABILITY_API_KEY\n\nif (!apiKey) throw new Error('Missing Stability API key.')\n\n// NOTE: This example is using a NodeJS FormData library.\n// Browsers should use their native FormData class.\n// React Native apps should also use their native FormData class.\nconst formData = new FormData()\nformData.append('image', fs.readFileSync('../init_image.png'))\nformData.append('width', 1024)\n\nconst response = await fetch(\n `${apiHost}/v1/generation/${engineId}/image-to-image/upscale`,\n {\n method: 'POST',\n headers: {\n ...formData.getHeaders(),\n Accept: 'image/png',\n Authorization: `Bearer ${apiKey}`,\n },\n body: formData,\n }\n)\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\nconst image = await response.arrayBuffer()\nfs.writeFileSync('./out/v1_upscaled_image.png', Buffer.from(image))\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"mime/multipart\"\n\t\"net/http\"\n\t\"os\"\n)\n\nfunc main() {\n\tengineId := \"esrgan-v1-x2plus\"\n\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/generation/\" + engineId + \"/image-to-image/upscale\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\tdata := &bytes.Buffer{}\n\twriter := multipart.NewWriter(data)\n\n\t// Write the init image to the request\n\tinitImageWriter, _ := writer.CreateFormField(\"image\")\n\tinitImageFile, initImageErr := os.Open(\"../init_image.png\")\n\tif initImageErr != nil {\n\t\tpanic(\"Could not open init_image.png\")\n\t}\n\t_, _ = io.Copy(initImageWriter, initImageFile)\n\n\t// Write the options to the request\n\t_ = writer.WriteField(\"width\", \"1024\")\n\twriter.Close()\n\n\t// Execute the request\n\tpayload := bytes.NewReader(data.Bytes())\n\treq, _ := http.NewRequest(\"POST\", reqUrl, payload)\n\treq.Header.Add(\"Content-Type\", writer.FormDataContentType())\n\treq.Header.Add(\"Accept\", \"image/png\")\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\n\tif res.StatusCode != 200 {\n\t\tvar body map[string]interface{}\n\t\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tpanic(fmt.Sprintf(\"Non-200 response: %s\", body))\n\t}\n\n\t// Write the response to a file\n\tout, err := os.Create(\"./out/v1_upscaled_image.png\")\n\tdefer out.Close()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t_, err = io.Copy(out, res.Body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\nOUTPUT_FILE=./out/v1_upscaled_image.png\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/generation/esrgan-v1-x2plus/image-to-image/upscale\"\n\ncurl -f -sS -X POST \"$URL\" \\\n -H 'Content-Type: multipart/form-data' \\\n -H 'Accept: image/png' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\" \\\n -F 'image=@\"../init_image.png\"' \\\n -F 'width=1024' \\\n -o \"$OUTPUT_FILE\"\n"
}
]
}
},
"/v1/generation/{engine_id}/image-to-image/masking": {
"post": {
"description": "Selectively modify portions of an image using a mask",
"operationId": "masking",
"summary": "image-to-image/masking",
"tags": [
"v1/generation"
],
"parameters": [
{
"example": "stable-inpainting-512-v2-0",
"in": "path",
"name": "engine_id",
"required": true,
"schema": {
"type": "string"
}
},
{
"$ref": "#/components/parameters/accept"
},
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/MaskingRequestBody"
},
"examples": {
"MASK_IMAGE_BLACK": {
"value": {
"mask_source": "MASK_IMAGE_BLACK",
"init_image": "<image binary>",
"mask_image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 3,
"steps": 20
}
},
"MASK_IMAGE_WHITE": {
"value": {
"mask_source": "MASK_IMAGE_WHITE",
"init_image": "<image binary>",
"mask_image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 3,
"steps": 20
}
},
"INIT_IMAGE_ALPHA": {
"value": {
"mask_source": "INIT_IMAGE_ALPHA",
"init_image": "<image binary>",
"text_prompts[0][text]": "A dog space commander",
"text_prompts[0][weight]": 1,
"cfg_scale": 7,
"clip_guidance_preset": "FAST_BLUE",
"sampler": "K_DPM_2_ANCESTRAL",
"samples": 3,
"steps": 20
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK response.",
"content": {
"application/json": {
"schema": {
"description": "The result of the generation request, containing one or more images as base64 encoded strings.",
"items": {
"$ref": "#/components/schemas/Image"
},
"type": "array"
}
},
"image/png": {
"example": "PNG bytes, what did you expect?",
"schema": {
"description": "The bytes of the generated image",
"format": "binary",
"type": "string"
}
}
},
"headers": {
"Content-Length": {
"$ref": "#/components/headers/Content-Length"
},
"Content-Type": {
"$ref": "#/components/headers/Content-Type"
},
"Finish-Reason": {
"$ref": "#/components/headers/Finish-Reason"
},
"Seed": {
"$ref": "#/components/headers/Seed"
}
}
},
"400": {
"$ref": "#/components/responses/400FromGeneration"
},
"401": {
"$ref": "#/components/responses/401"
},
"403": {
"$ref": "#/components/responses/403"
},
"404": {
"$ref": "#/components/responses/404"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import base64\nimport os\nimport requests\n\nengine_id = \"stable-inpainting-512-v2-0\"\napi_host = os.getenv('API_HOST', 'https://api.stability.ai')\napi_key = os.getenv(\"STABILITY_API_KEY\")\n\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.post(\n f\"{api_host}/v1/generation/{engine_id}/image-to-image/masking\",\n headers={\n \"Accept\": 'application/json',\n \"Authorization\": f\"Bearer {api_key}\"\n },\n files={\n 'init_image': open(\"../init_image.png\", 'rb'),\n 'mask_image': open(\"../mask_image_white.png\", 'rb'),\n },\n data={\n \"mask_source\": \"MASK_IMAGE_WHITE\",\n \"text_prompts[0][text]\": \"A large spiral galaxy with a bright central bulge and a ring of stars around it\",\n \"cfg_scale\": 7,\n \"clip_guidance_preset\": \"FAST_BLUE\",\n \"samples\": 1,\n \"steps\": 30,\n }\n)\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\ndata = response.json()\n\nfor i, image in enumerate(data[\"artifacts\"]):\n with open(f\"./out/v1_img2img_masking_{i}.png\", \"wb\") as f:\n f.write(base64.b64decode(image[\"base64\"]))\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\nimport FormData from 'form-data'\nimport fs from 'node:fs'\n\nconst engineId = 'stable-inpainting-512-v2-0'\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst apiKey = process.env.STABILITY_API_KEY\n\nif (!apiKey) throw new Error('Missing Stability API key.')\n\n// NOTE: This example is using a NodeJS FormData library. Browser\n// implementations should use their native FormData class. React Native\n// implementations should also use their native FormData class.\nconst formData = new FormData()\nformData.append('init_image', fs.readFileSync('../init_image.png'))\nformData.append('mask_image', fs.readFileSync('../mask_image_white.png'))\nformData.append('mask_source', 'MASK_IMAGE_WHITE')\nformData.append(\n 'text_prompts[0][text]',\n 'A large spiral galaxy with a bright central bulge and a ring of stars around it'\n)\nformData.append('cfg_scale', '7')\nformData.append('clip_guidance_preset', 'FAST_BLUE')\nformData.append('samples', 1)\nformData.append('steps', 30)\n\nconst response = await fetch(\n `${apiHost}/v1/generation/${engineId}/image-to-image/masking`,\n {\n method: 'POST',\n headers: {\n ...formData.getHeaders(),\n Accept: 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: formData,\n }\n)\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface GenerationResponse {\n artifacts: Array<{\n base64: string\n seed: number\n finishReason: string\n }>\n}\n\nconst responseJSON = (await response.json()) as GenerationResponse\n\nresponseJSON.artifacts.forEach((image, index) => {\n fs.writeFileSync(\n `out/v1_img2img_masking_${index}.png`,\n Buffer.from(image.base64, 'base64')\n )\n})\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"bytes\"\n\t\"encoding/base64\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"mime/multipart\"\n\t\"net/http\"\n\t\"os\"\n)\n\ntype MaskingImage struct {\n\tBase64 string `json:\"base64\"`\n\tSeed uint32 `json:\"seed\"`\n\tFinishReason string `json:\"finishReason\"`\n}\n\ntype MaskingResponse struct {\n\tImages []MaskingImage `json:\"artifacts\"`\n}\n\nfunc main() {\n\tengineId := \"stable-inpainting-512-v2-0\"\n\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/generation/\" + engineId + \"/image-to-image/masking\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\tdata := &bytes.Buffer{}\n\twriter := multipart.NewWriter(data)\n\n\t// Write the init image to the request\n\tinitImageWriter, _ := writer.CreateFormField(\"init_image\")\n\tinitImageFile, initImageErr := os.Open(\"../init_image.png\")\n\tif initImageErr != nil {\n\t\tpanic(\"Could not open init_image.png\")\n\t}\n\t_, _ = io.Copy(initImageWriter, initImageFile)\n\n\t// Write the mask image to the request\n\tmaskImageWriter, _ := writer.CreateFormField(\"mask_image\")\n\tmaskImageFile, maskImageErr := os.Open(\"../mask_image_white.png\")\n\tif maskImageErr != nil {\n\t\tpanic(\"Could not open mask_image_white.png\")\n\t}\n\t_, _ = io.Copy(maskImageWriter, maskImageFile)\n\n\t// Write the options to the request\n\t_ = writer.WriteField(\"mask_source\", \"MASK_IMAGE_WHITE\")\n\t_ = writer.WriteField(\"text_prompts[0][text]\", \"A large spiral galaxy with a bright central bulge and a ring of stars around it\")\n\t_ = writer.WriteField(\"cfg_scale\", \"7\")\n\t_ = writer.WriteField(\"clip_guidance_preset\", \"FAST_BLUE\")\n\t_ = writer.WriteField(\"samples\", \"1\")\n\t_ = writer.WriteField(\"steps\", \"30\")\n\twriter.Close()\n\n\t// Execute the request & read all the bytes of the response\n\tpayload := bytes.NewReader(data.Bytes())\n\treq, _ := http.NewRequest(\"POST\", reqUrl, payload)\n\treq.Header.Add(\"Content-Type\", writer.FormDataContentType())\n\treq.Header.Add(\"Accept\", \"application/json\")\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\n\tif res.StatusCode != 200 {\n\t\tvar body map[string]interface{}\n\t\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tpanic(fmt.Sprintf(\"Non-200 response: %s\", body))\n\t}\n\n\t// Decode the JSON body\n\tvar body MaskingResponse\n\tif err := json.NewDecoder(res.Body).Decode(&body); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Write the images to disk\n\tfor i, image := range body.Images {\n\t\toutFile := fmt.Sprintf(\"./out/v1_img2img_masking_%d.png\", i)\n\t\tfile, err := os.Create(outFile)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\timageBytes, err := base64.StdEncoding.DecodeString(image.Base64)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif _, err := file.Write(imageBytes); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\n\t\tif err := file.Close(); err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t}\n}\n"
},
{
"lang": "cURL",
"source": "#!/bin/sh\n\nset -e\n\nif [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\nOUTPUT_FILE=./out/v1_img2img_masking.png\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/generation/stable-inpainting-512-v2-0/image-to-image/masking\"\n\ncurl -f -sS -X POST \"$URL\" \\\n -H 'Content-Type: multipart/form-data' \\\n -H 'Accept: image/png' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\" \\\n -F 'init_image=@\"../init_image.png\"' \\\n -F 'mask_image=@\"../mask_image_white.png\"' \\\n -F 'mask_source=MASK_IMAGE_WHITE' \\\n -F 'text_prompts[0][text]=A large spiral galaxy with a bright central bulge and a ring of stars around it' \\\n -F 'cfg_scale=7' \\\n -F 'clip_guidance_preset=FAST_BLUE' \\\n -F 'samples=1' \\\n -F 'steps=30' \\\n -o \"$OUTPUT_FILE\"\n"
}
]
}
},
"/v1/engines/list": {
"get": {
"description": "List all engines available to your organization/user",
"operationId": "listEngines",
"summary": "list",
"tags": [
"v1/engines"
],
"parameters": [
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ListEnginesResponseBody"
}
}
},
"description": "OK response."
},
"401": {
"$ref": "#/components/responses/401"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import os\nimport requests\n\napi_host = os.getenv('API_HOST', 'https://api.stability.ai')\nurl = f\"{api_host}/v1/engines/list\"\n\napi_key = os.getenv(\"STABILITY_API_KEY\")\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.get(url, headers={\n \"Authorization\": f\"Bearer {api_key}\"\n})\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\n# Do something with the payload...\npayload = response.json()\n\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\n\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst url = `${apiHost}/v1/engines/list`\n\nconst apiKey = process.env.STABILITY_API_KEY\nif (!apiKey) throw new Error('Missing Stability API key.')\n\nconst response = await fetch(url, {\n method: 'GET',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n },\n})\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface Payload {\n engines: Array<{\n id: string\n name: string\n description: string\n type: string\n }>\n}\n\n// Do something with the payload...\nconst payload = (await response.json()) as Payload\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\nfunc main() {\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/engines/list\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\t// Execute the request & read all the bytes of the response\n\treq, _ := http.NewRequest(\"GET\", reqUrl, nil)\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\tbody, _ := io.ReadAll(res.Body)\n\n\tif res.StatusCode != 200 {\n\t\tpanic(\"Non-200 response: \" + string(body))\n\t}\n\n\t// Do something with the payload...\n\t// payload := string(body)\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/engines/list\"\n\ncurl -f -sS \"$URL\" \\\n -H 'Accept: application/json' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\"\n"
}
]
}
},
"/v1/user/account": {
"get": {
"description": "Get information about the account associated with the provided API key",
"operationId": "userAccount",
"summary": "account",
"tags": [
"v1/user"
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AccountResponseBody"
}
}
},
"description": "OK response."
},
"401": {
"$ref": "#/components/responses/401"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import os\nimport requests\n\napi_host = os.getenv('API_HOST', 'https://api.stability.ai')\nurl = f\"{api_host}/v1/user/account\"\n\napi_key = os.getenv(\"STABILITY_API_KEY\")\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.get(url, headers={\n \"Authorization\": f\"Bearer {api_key}\"\n})\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\n# Do something with the payload...\npayload = response.json()\n\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\n\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst url = `${apiHost}/v1/user/account`\n\nconst apiKey = process.env.STABILITY_API_KEY\nif (!apiKey) throw new Error('Missing Stability API key.')\n\nconst response = await fetch(url, {\n method: 'GET',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n },\n})\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface User {\n id: string\n profile_picture: string\n email: string\n organizations?: Array<{\n id: string\n name: string\n role: string\n is_default: boolean\n }>\n}\n\n// Do something with the user...\nconst user = (await response.json()) as User\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\nfunc main() {\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/user/account\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\t// Build the request\n\treq, _ := http.NewRequest(\"GET\", reqUrl, nil)\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\n\t// Execute the request\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\tbody, _ := io.ReadAll(res.Body)\n\n\tif res.StatusCode != 200 {\n\t\tpanic(\"Non-200 response: \" + string(body))\n\t}\n\n\t// Do something with the payload...\n\t// payload := string(body)\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\n# Determine the URL to use for the request\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/user/account\"\n\ncurl -f -sS \"$URL\" \\\n -H 'Accept: application/json' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\"\n"
}
]
}
},
"/v1/user/balance": {
"get": {
"description": "Get the credit balance of the account/organization associated with the API key",
"operationId": "userBalance",
"summary": "balance",
"tags": [
"v1/user"
],
"parameters": [
{
"$ref": "#/components/parameters/organization"
},
{
"$ref": "#/components/parameters/stabilityClientID"
},
{
"$ref": "#/components/parameters/stabilityClientVersion"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"example": {
"credits": 0.6336833840314097
},
"schema": {
"$ref": "#/components/schemas/BalanceResponseBody"
}
}
},
"description": "OK response."
},
"401": {
"$ref": "#/components/responses/401"
},
"500": {
"$ref": "#/components/responses/500"
}
},
"security": [
{
"STABILITY_API_KEY": []
}
],
"x-codeSamples": [
{
"lang": "Python",
"source": "import os\nimport requests\n\napi_host = os.getenv('API_HOST', 'https://api.stability.ai')\nurl = f\"{api_host}/v1/user/balance\"\n\napi_key = os.getenv(\"STABILITY_API_KEY\")\nif api_key is None:\n raise Exception(\"Missing Stability API key.\")\n\nresponse = requests.get(url, headers={\n \"Authorization\": f\"Bearer {api_key}\"\n})\n\nif response.status_code != 200:\n raise Exception(\"Non-200 response: \" + str(response.text))\n\n# Do something with the payload...\npayload = response.json()\n\n"
},
{
"label": "TypeScript",
"lang": "Javascript",
"source": "import fetch from 'node-fetch'\n\nconst apiHost = process.env.API_HOST ?? 'https://api.stability.ai'\nconst url = `${apiHost}/v1/user/balance`\n\nconst apiKey = process.env.STABILITY_API_KEY\nif (!apiKey) throw new Error('Missing Stability API key.')\n\nconst response = await fetch(url, {\n method: 'GET',\n headers: {\n Authorization: `Bearer ${apiKey}`,\n },\n})\n\nif (!response.ok) {\n throw new Error(`Non-200 response: ${await response.text()}`)\n}\n\ninterface Balance {\n credits: number\n}\n\n// Do something with the balance...\nconst balance = (await response.json()) as Balance\n"
},
{
"lang": "Go",
"source": "package main\n\nimport (\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n)\n\nfunc main() {\n\t// Build REST endpoint URL\n\tapiHost, hasApiHost := os.LookupEnv(\"API_HOST\")\n\tif !hasApiHost {\n\t\tapiHost = \"https://api.stability.ai\"\n\t}\n\treqUrl := apiHost + \"/v1/user/balance\"\n\n\t// Acquire an API key from the environment\n\tapiKey, hasAPIKey := os.LookupEnv(\"STABILITY_API_KEY\")\n\tif !hasAPIKey {\n\t\tpanic(\"Missing STABILITY_API_KEY environment variable\")\n\t}\n\n\t// Build the request\n\treq, _ := http.NewRequest(\"GET\", reqUrl, nil)\n\treq.Header.Add(\"Authorization\", \"Bearer \"+apiKey)\n\n\t// Execute the request\n\tres, _ := http.DefaultClient.Do(req)\n\tdefer res.Body.Close()\n\tbody, _ := io.ReadAll(res.Body)\n\n\tif res.StatusCode != 200 {\n\t\tpanic(\"Non-200 response: \" + string(body))\n\t}\n\n\t// Do something with the payload...\n\t// payload := string(body)\n}\n"
},
{
"lang": "cURL",
"source": "if [ -z \"$STABILITY_API_KEY\" ]; then\n echo \"STABILITY_API_KEY environment variable is not set\"\n exit 1\nfi\n\n# Determine the URL to use for the request\nBASE_URL=${API_HOST:-https://api.stability.ai}\nURL=\"$BASE_URL/v1/user/balance\"\n\ncurl -f -sS \"$URL\" \\\n -H 'Content-Type: application/json' \\\n -H \"Authorization: Bearer $STABILITY_API_KEY\"\n"
}
]
}
}
},
"components": {
"schemas": {
"Engine": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string",
"x-go-name": "ID",
"description": "Unique identifier for the engine",
"example": "stable-diffusion-v1-5"
},
"name": {
"type": "string",
"description": "Name of the engine",
"example": "Stable Diffusion v1.5"
},
"type": {
"type": "string",
"description": "The type of content this engine produces",
"example": "PICTURE",
"enum": [
"AUDIO",
"CLASSIFICATION",
"PICTURE",
"STORAGE",
"TEXT",
"VIDEO"
]
}
},
"required": [
"id",
"name",
"description",
"type"
]
},
"Error": {
"type": "object",
"x-go-name": "RESTError",
"properties": {
"id": {
"x-go-name": "ID",
"type": "string",
"description": "A unique identifier for this particular occurrence of the problem.",
"example": "296a972f-666a-44a1-a3df-c9c28a1f56c0"
},
"name": {
"type": "string",
"description": "The short-name of this class of errors e.g. `bad_request`.",
"example": "bad_request"
},
"message": {
"type": "string",
"description": "A human-readable explanation specific to this occurrence of the problem.",
"example": "Header parameter Authorization is required, but not found"
}
},
"required": [
"name",
"id",
"message",
"status"
]
},
"CfgScale": {
"type": "number",
"description": "How strictly the diffusion process adheres to the prompt text (higher values keep your image closer to your prompt)",
"default": 7,
"example": 7,
"minimum": 0,
"maximum": 35
},
"ClipGuidancePreset": {
"type": "string",
"default": "NONE",
"example": "FAST_BLUE",
"enum": [
"FAST_BLUE",
"FAST_GREEN",
"NONE",
"SIMPLE",
"SLOW",
"SLOWER",
"SLOWEST"
]
},
"UpscaleImageHeight": {
"x-go-type": "uint64",
"type": "integer",
"description": "Desired height of the output image. Only one of `width` or `height` may be specified.",
"minimum": 512
},
"UpscaleImageWidth": {
"x-go-type": "uint64",
"type": "integer",
"description": "Desired width of the output image. Only one of `width` or `height` may be specified.",
"minimum": 512
},
"DiffuseImageHeight": {
"x-go-type": "uint64",
"type": "integer",
"description": "Height of the image in pixels. Must be in increments of 64 and pass the following validation:\n- For 512 engines: 262,144 ≤ `height * width` ≤ 1,048,576\n- For 768 engines: 589,824 ≤ `height * width` ≤ 1,048,576\n- For SDXL Beta: can be as low as 128 and as high as 896 as long as `width` is not greater than 512. If `width` is greater than 512 then this can be _at most_ 512.\n- For SDXL v0.9: valid dimensions are 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640, 640x1536, 768x1344, 832x1216, or 896x1152\n- For SDXL v1.0: valid dimensions are the same as SDXL v0.9",
"multipleOf": 64,
"default": 512,
"example": 512,
"minimum": 128
},
"DiffuseImageWidth": {
"x-go-type": "uint64",
"type": "integer",
"description": "Width of the image in pixels. Must be in increments of 64 and pass the following validation:\n- For 512 engines: 262,144 ≤ `height * width` ≤ 1,048,576\n- For 768 engines: 589,824 ≤ `height * width` ≤ 1,048,576\n- For SDXL Beta: can be as low as 128 and as high as 896 as long as `height` is not greater than 512. If `height` is greater than 512 then this can be _at most_ 512.\n- For SDXL v0.9: valid dimensions are 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640, 640x1536, 768x1344, 832x1216, or 896x1152\n- For SDXL v1.0: valid dimensions are the same as SDXL v0.9",
"multipleOf": 64,
"default": 512,
"example": 512,
"minimum": 128
},
"Sampler": {
"type": "string",
"description": "Which sampler to use for the diffusion process. If this value is omitted we'll automatically select an appropriate sampler for you.",
"example": "K_DPM_2_ANCESTRAL",
"enum": [
"DDIM",
"DDPM",
"K_DPMPP_2M",
"K_DPMPP_2S_ANCESTRAL",
"K_DPM_2",
"K_DPM_2_ANCESTRAL",
"K_EULER",
"K_EULER_ANCESTRAL",
"K_HEUN",
"K_LMS"
]
},
"Samples": {
"x-go-type": "uint64",
"type": "integer",
"description": "Number of images to generate",
"default": 1,
"example": 1,
"minimum": 1,
"maximum": 10
},
"Seed": {
"type": "integer",
"x-go-type": "uint32",
"description": "Random noise seed (omit this option or use `0` for a random seed)",
"default": 0,
"example": 0,
"minimum": 0,
"maximum": 4294967295
},
"Steps": {
"x-go-type": "uint64",
"type": "integer",
"description": "Number of diffusion steps to run",
"default": 50,
"example": 75,
"minimum": 10,
"maximum": 150
},
"Extras": {
"type": "object",
"description": "Extra parameters passed to the engine.\nThese parameters are used for in-development or experimental features and may change\nwithout warning, so please use with caution."
},
"StylePreset": {
"type": "string",
"enum": [
"3d-model",
"analog-film",
"anime",
"cinematic",
"comic-book",
"digital-art",
"enhance",
"fantasy-art",
"isometric",
"line-art",
"low-poly",
"modeling-compound",
"neon-punk",
"origami",
"photographic",
"pixel-art",
"tile-texture"
],
"description": "Pass in a style preset to guide the image model towards a particular style.\nThis list of style presets is subject to change."
},
"TextPrompt": {