Skip to content

How to Create an App Using V3 of the CC API

Greg Cobb edited this page Mar 13, 2017 · 42 revisions
  1. Find the GUID of your space and set an APP_NAME:
CF_API_ENDPOINT=$(cf api | grep "API endpoint" | awk '{print $3}')
SPACE_GUID=$(cf space `cf target | tail -n 1 | awk '{print $2}'` --guid)
APP_NAME=[your-app-name]
  1. Create an empty app (docs):

Choose one of the following methods of creating the app for defaults, custom buildpack / stack, or docker application.

APP_GUID=$(cf curl /v3/apps -X POST -d '{"name":"'$APP_NAME'","relationships": {"space": {"guid": "'$SPACE_GUID'"} } }' | tee /dev/tty | jq -r .guid)
APP_GUID=$(cf curl /v3/apps -X POST -d '{"name":"'$APP_NAME'",
  "relationships": {"space": {"guid": "'$SPACE_GUID'"} },
  "lifecycle": { "type": "buildpack", "data": { "stack": "cflinuxfs2", "buildpacks": ["ruby_buildpack"]} }' | tee /dev/tty | jq -r .guid)
APP_GUID=$(cf curl /v3/apps -X POST -d '{"name":"'$APP_NAME'",
  "relationships": {"space": {"guid": "'$SPACE_GUID'"} },
  "lifecycle": { "type": "docker", "data": {} } }' | tee /dev/tty | jq -r .guid)
  1. Create an empty package for the app (docs):
PACKAGE_GUID=$(cf curl /v3/apps/$APP_GUID/packages -X POST -d '{"type":"bits"}' | tee /dev/tty | jq -r .guid)
DOCKER_IMAGE=registry/image:latest
PACKAGE_GUID=$(cf curl /v3/apps/$APP_GUID/packages -X POST -d '{"type":"docker",
  "data": {
  "image": "'$DOCKER_IMAGE'" 
}}' | tee /dev/tty | jq -r .guid)

Note: The output of this command includes your new package's GUID
Note: Other package types are also supported. See documentation for Create a Package.

  1. If your package is type buildpack, create a ZIP file of your application:

zip -r my-app.zip *

Note: The zip file should not have a folder as the top-level item (e.g. create the zip file from within your app’s directory)

  1. If your package is type buildpack, upload your bits to your new package (docs):

curl $CF_API_ENDPOINT/v3/packages/$PACKAGE_GUID/upload -F bits=@"my-app.zip" -H "Authorization: `cf oauth-token | grep bearer`"

  1. Stage your package and create a droplet (docs):

DROPLET_GUID=$(cf curl /v3/packages/$PACKAGE_GUID/droplets -X POST -d '{}' | tee /dev/tty | jq -r .guid)

Note: The output of this command includes your new droplet's GUID

  1. Assign your droplet to your app (docs):

cf curl /v3/apps/$APP_GUID/droplets/current -X PUT -d '{"droplet_guid": "'$DROPLET_GUID'"}'

  1. Create a route:
CF_TRACE=true cf create-route [space-name] [domain-name] -n [host-name]
ROUTE_GUID=[copy route's GUID under metadata->guid of the last response]
  1. Map the route to your app (docs):

cf curl /v3/route_mappings -X POST -d '{"relationships": {"route": {"guid": "'$ROUTE_GUID'"}, "app": {"guid": "'$APP_GUID'"} } }'

  1. Start your app (docs):

cf curl /v3/apps/$APP_GUID/start -X PUT

  1. Visit your app at the new route to confirm that it is up
Clone this wiki locally