API: Create project

I want to create some page projects using the the API but am confused about the syntax.

An example is given:

curl -X POST "https://api.cloudflare.com/client/v4/accounts/:account_id/pages/projects" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"name":"NextJS Blog","id":"7b162ea7-7367-4d67-bcde-1160995d5","created_on":"2017-01-01T00:00:00Z","subdomain":"helloworld.pages.dev","domains":["customdomain.com","customdomain.org"],"source":{"type":"github","config":{"owner":"cloudflare","repo_name":"ninjakittens","production_branch":"main","pr_comments_enabled":true,"deployments_enabled":true}},"build_config":{"build_command":"npm run build","destination_dir":"build","root_dir":"/","web_analytics_tag":"cee1c73f6e4743d0b5e6bb1a0bcaabcc","web_analytics_token":"021e1057c18547eca7b79f2516f06o7x"},"deployment_configs":{"preview":{"env_vars":{"BUILD_VERSION":{"value":"3.3"}}},"production":{"env_vars":{"BUILD_VERSION":{"value":"3.3"}}}},"latest_deployment":{"id":"f64788e9-fccd-4d4a-a28a-cb84f88f6","short_id":"f64788e9","project_id":"7b162ea7-7367-4d67-bcde-1160995d5","project_name":"ninjakittens","environment":"preview","url":"https://f64788e9.ninjakittens.pages.dev","created_on":"2021-03-09T00:55:03.923456Z","modified_on":"2021-03-09T00:58:59.045655","aliases":["https://branchname.projectname.pages.dev"],"latest_stage":{"name":"deploy","started_on":"2021-03-09T00:55:03.923456Z","ended_on":"2021-03-09T00:58:59.045655","status":"success"},"env_vars":{"BUILD_VERSION":{"value":"3.3"},"ENV":{"value":"STAGING"}},"deployment_trigger":{"type":"ad_hoc","metadata":{"branch":"main","commit_hash":"ad9ccd918a81025731e10e40267e11273a263421","commit_message":"Update index.html"}},"stages":[{"name":"queued","started_on":"2021-06-03T15:38:15.608194Z","ended_on":"2021-06-03T15:39:03.134378Z","status":"active"},{"name":"initialize","started_on":null,"ended_on":null,"status":"idle"},{"name":"clone_repo","started_on":null,"ended_on":null,"status":"idle"},{"name":"build","started_on":null,"ended_on":null,"status":"idle"},{"name":"deploy","started_on":null,"ended_on":null,"status":"idle"}],"build_config":{"build_command":"npm run build","destination_dir":"build","root_dir":"/","web_analytics_tag":"cee1c73f6e4743d0b5e6bb1a0bcaabcc","web_analytics_token":"021e1057c18547eca7b79f2516f06o7x"},"source":{"type":"github","config":{"owner":"cloudflare","repo_name":"ninjakittens","production_branch":"main","pr_comments_enabled":true,"deployments_enabled":true}}},"canonical_deployment":{"id":"f64788e9-fccd-4d4a-a28a-cb84f88f6","short_id":"f64788e9","project_id":"7b162ea7-7367-4d67-bcde-1160995d5","project_name":"ninjakittens","environment":"preview","url":"https://f64788e9.ninjakittens.pages.dev","created_on":"2021-03-09T00:55:03.923456Z","modified_on":"2021-03-09T00:58:59.045655","aliases":["https://branchname.projectname.pages.dev"],"latest_stage":{"name":"deploy","started_on":"2021-03-09T00:55:03.923456Z","ended_on":"2021-03-09T00:58:59.045655","status":"success"},"env_vars":{"BUILD_VERSION":{"value":"3.3"},"ENV":{"value":"STAGING"}},"deployment_trigger":{"type":"ad_hoc","metadata":{"branch":"main","commit_hash":"ad9ccd918a81025731e10e40267e11273a263421","commit_message":"Update index.html"}},"stages":[{"name":"queued","started_on":"2021-06-03T15:38:15.608194Z","ended_on":"2021-06-03T15:39:03.134378Z","status":"active"},{"name":"initialize","started_on":null,"ended_on":null,"status":"idle"},{"name":"clone_repo","started_on":null,"ended_on":null,"status":"idle"},{"name":"build","started_on":null,"ended_on":null,"status":"idle"},{"name":"deploy","started_on":null,"ended_on":null,"status":"idle"}],"build_config":{"build_command":"npm run build","destination_dir":"build","root_dir":"/","web_analytics_tag":"cee1c73f6e4743d0b5e6bb1a0bcaabcc","web_analytics_token":"021e1057c18547eca7b79f2516f06o7x"},"source":{"type":"github","config":{"owner":"cloudflare","repo_name":"ninjakittens","production_branch":"main","pr_comments_enabled":true,"deployments_enabled":true}}}}'

The strange part is that an “id” is specified, but this is a read-only value and can’t exist yet for something which is being created.

Should I just leave this field out, or provide a null, or do I need to create the id value first somehow?

You should test, but I think you should be able to leave the id part out when doing a POST (create) and the JSON object returned by the API when you successfully create it will return the id assigned to your project, which you can then use to edit (PUT) or delete (DELETE).

I will flag that code example up to the team internally, as I agree it’s unclear.

1 Like

Thanks, it worked fine without the id.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.