|
| 1 | +# Getting Started with Cloud Foundry CAPI v3 |
| 2 | + |
| 3 | +Welcome to the Cloud Foundry Cloud Controller API (CAPI) v3! This guide will help you get started with using the API to manage applications, services, and other Cloud Foundry resources. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you begin, ensure you have: |
| 8 | + |
| 9 | +1. **Access to a Cloud Foundry deployment** - You'll need the API endpoint URL |
| 10 | +2. **Valid credentials** - Either a username/password or a client ID/secret |
| 11 | +3. **CF CLI installed** (optional but recommended) - For obtaining authentication tokens |
| 12 | +4. **An HTTP client** - Such as curl, Postman, or a programming language with HTTP support |
| 13 | + |
| 14 | +## API Endpoint |
| 15 | + |
| 16 | +The Cloud Foundry API is typically available at: |
| 17 | +``` |
| 18 | +https://api.<your-cf-domain> |
| 19 | +``` |
| 20 | + |
| 21 | +You can verify the API endpoint and version by accessing the root endpoint: |
| 22 | +```bash |
| 23 | +curl https://api.example.com/ |
| 24 | +``` |
| 25 | + |
| 26 | +## Authentication |
| 27 | + |
| 28 | +All API requests (except `/` and `/v3/info`) require authentication using a Bearer token. |
| 29 | + |
| 30 | +### Obtaining a Token |
| 31 | + |
| 32 | +#### Using CF CLI (Recommended) |
| 33 | +```bash |
| 34 | +# Login |
| 35 | +cf login -a https://api.example.com |
| 36 | + |
| 37 | +# Get your access token |
| 38 | +cf oauth-token |
| 39 | +``` |
| 40 | + |
| 41 | +The token will be in the format: `bearer <token>`. Use the entire string in your API requests. |
| 42 | + |
| 43 | +#### Direct UAA Authentication |
| 44 | +```bash |
| 45 | +# Get token endpoint from API info |
| 46 | +curl https://api.example.com/v3/info |
| 47 | + |
| 48 | +# Exchange credentials for token |
| 49 | +curl -X POST https://uaa.example.com/oauth/token \ |
| 50 | + -H "Content-Type: application/x-www-form-urlencoded" \ |
| 51 | + -d "grant_type=password&username=<user>&password=<pass>&client_id=cf&client_secret=" |
| 52 | +``` |
| 53 | + |
| 54 | +## Your First API Calls |
| 55 | + |
| 56 | +### 1. Get API Information |
| 57 | +```bash |
| 58 | +curl https://api.example.com/v3/info \ |
| 59 | + -H "Accept: application/json" |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. List Organizations |
| 63 | +```bash |
| 64 | +curl https://api.example.com/v3/organizations \ |
| 65 | + -H "Authorization: bearer <your-token>" \ |
| 66 | + -H "Accept: application/json" |
| 67 | +``` |
| 68 | + |
| 69 | +### 3. List Applications |
| 70 | +```bash |
| 71 | +curl https://api.example.com/v3/apps \ |
| 72 | + -H "Authorization: bearer <your-token>" \ |
| 73 | + -H "Accept: application/json" |
| 74 | +``` |
| 75 | + |
| 76 | +## Common Operations |
| 77 | + |
| 78 | +### Creating an Application |
| 79 | + |
| 80 | +```bash |
| 81 | +curl -X POST https://api.example.com/v3/apps \ |
| 82 | + -H "Authorization: bearer <your-token>" \ |
| 83 | + -H "Content-Type: application/json" \ |
| 84 | + -d '{ |
| 85 | + "name": "my-app", |
| 86 | + "relationships": { |
| 87 | + "space": { |
| 88 | + "data": { |
| 89 | + "guid": "<space-guid>" |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }' |
| 94 | +``` |
| 95 | + |
| 96 | +### Uploading Application Code |
| 97 | + |
| 98 | +1. **Create a package**: |
| 99 | +```bash |
| 100 | +curl -X POST https://api.example.com/v3/packages \ |
| 101 | + -H "Authorization: bearer <your-token>" \ |
| 102 | + -H "Content-Type: application/json" \ |
| 103 | + -d '{ |
| 104 | + "type": "bits", |
| 105 | + "relationships": { |
| 106 | + "app": { |
| 107 | + "data": { |
| 108 | + "guid": "<app-guid>" |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + }' |
| 113 | +``` |
| 114 | + |
| 115 | +2. **Upload bits to the package**: |
| 116 | +```bash |
| 117 | +curl -X POST https://api.example.com/v3/packages/<package-guid>/upload \ |
| 118 | + -H "Authorization: bearer <your-token>" \ |
| 119 | + |
| 120 | +``` |
| 121 | + |
| 122 | +3. **Create a build**: |
| 123 | +```bash |
| 124 | +curl -X POST https://api.example.com/v3/builds \ |
| 125 | + -H "Authorization: bearer <your-token>" \ |
| 126 | + -H "Content-Type: application/json" \ |
| 127 | + -d '{ |
| 128 | + "package": { |
| 129 | + "guid": "<package-guid>" |
| 130 | + } |
| 131 | + }' |
| 132 | +``` |
| 133 | + |
| 134 | +### Starting an Application |
| 135 | + |
| 136 | +```bash |
| 137 | +curl -X POST https://api.example.com/v3/apps/<app-guid>/actions/start \ |
| 138 | + -H "Authorization: bearer <your-token>" |
| 139 | +``` |
| 140 | + |
| 141 | +## Understanding Responses |
| 142 | + |
| 143 | +### Successful Response |
| 144 | +```json |
| 145 | +{ |
| 146 | + "guid": "585bc3a1-3743-497d-88b0-403ad6b56d16", |
| 147 | + "name": "my-app", |
| 148 | + "state": "STARTED", |
| 149 | + "created_at": "2025-01-26T19:24:43Z", |
| 150 | + "updated_at": "2025-01-26T19:25:01Z", |
| 151 | + "lifecycle": { |
| 152 | + "type": "buildpack", |
| 153 | + "data": { |
| 154 | + "buildpacks": [], |
| 155 | + "stack": "cflinuxfs3" |
| 156 | + } |
| 157 | + }, |
| 158 | + "relationships": { |
| 159 | + "space": { |
| 160 | + "data": { |
| 161 | + "guid": "2f35885d-0c9d-4423-83ad-fd05066f8576" |
| 162 | + } |
| 163 | + } |
| 164 | + }, |
| 165 | + "metadata": { |
| 166 | + "labels": {}, |
| 167 | + "annotations": {} |
| 168 | + }, |
| 169 | + "links": { |
| 170 | + "self": { |
| 171 | + "href": "https://api.example.com/v3/apps/585bc3a1-3743-497d-88b0-403ad6b56d16" |
| 172 | + }, |
| 173 | + "processes": { |
| 174 | + "href": "https://api.example.com/v3/apps/585bc3a1-3743-497d-88b0-403ad6b56d16/processes" |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +### Error Response |
| 181 | +```json |
| 182 | +{ |
| 183 | + "errors": [ |
| 184 | + { |
| 185 | + "code": 10008, |
| 186 | + "title": "UnprocessableEntity", |
| 187 | + "detail": "The request is semantically invalid: space can't be blank" |
| 188 | + } |
| 189 | + ] |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +## Pagination |
| 194 | + |
| 195 | +List endpoints return paginated results: |
| 196 | + |
| 197 | +```json |
| 198 | +{ |
| 199 | + "pagination": { |
| 200 | + "total_results": 42, |
| 201 | + "total_pages": 5, |
| 202 | + "first": { |
| 203 | + "href": "https://api.example.com/v3/apps?page=1&per_page=10" |
| 204 | + }, |
| 205 | + "last": { |
| 206 | + "href": "https://api.example.com/v3/apps?page=5&per_page=10" |
| 207 | + }, |
| 208 | + "next": { |
| 209 | + "href": "https://api.example.com/v3/apps?page=2&per_page=10" |
| 210 | + } |
| 211 | + }, |
| 212 | + "resources": [ |
| 213 | + { |
| 214 | + "guid": "...", |
| 215 | + "name": "app1" |
| 216 | + } |
| 217 | + ] |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +Navigate through pages using the `page` query parameter: |
| 222 | +```bash |
| 223 | +curl "https://api.example.com/v3/apps?page=2&per_page=50" \ |
| 224 | + -H "Authorization: bearer <your-token>" |
| 225 | +``` |
| 226 | + |
| 227 | +## Filtering Results |
| 228 | + |
| 229 | +Use query parameters to filter results: |
| 230 | + |
| 231 | +```bash |
| 232 | +# Filter by name |
| 233 | +curl "https://api.example.com/v3/apps?names=my-app,other-app" \ |
| 234 | + -H "Authorization: bearer <your-token>" |
| 235 | + |
| 236 | +# Filter by space |
| 237 | +curl "https://api.example.com/v3/apps?space_guids=<space-guid>" \ |
| 238 | + -H "Authorization: bearer <your-token>" |
| 239 | + |
| 240 | +# Filter by label |
| 241 | +curl "https://api.example.com/v3/apps?label_selector=env=production" \ |
| 242 | + -H "Authorization: bearer <your-token>" |
| 243 | +``` |
| 244 | + |
| 245 | +## Asynchronous Operations |
| 246 | + |
| 247 | +Some operations return a job for tracking: |
| 248 | + |
| 249 | +```bash |
| 250 | +# Delete an app (returns a job) |
| 251 | +curl -X DELETE https://api.example.com/v3/apps/<app-guid> \ |
| 252 | + -H "Authorization: bearer <your-token>" |
| 253 | + |
| 254 | +# Response includes job URL |
| 255 | +{ |
| 256 | + "links": { |
| 257 | + "job": { |
| 258 | + "href": "https://api.example.com/v3/jobs/87250c2e-7c04-4b2d-b8f7-b8a1791bb106" |
| 259 | + } |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +# Poll the job |
| 264 | +curl https://api.example.com/v3/jobs/87250c2e-7c04-4b2d-b8f7-b8a1791bb106 \ |
| 265 | + -H "Authorization: bearer <your-token>" |
| 266 | +``` |
| 267 | + |
| 268 | +## Using Metadata |
| 269 | + |
| 270 | +Add labels and annotations to organize resources: |
| 271 | + |
| 272 | +```bash |
| 273 | +curl -X PATCH https://api.example.com/v3/apps/<app-guid> \ |
| 274 | + -H "Authorization: bearer <your-token>" \ |
| 275 | + -H "Content-Type: application/json" \ |
| 276 | + -d '{ |
| 277 | + "metadata": { |
| 278 | + "labels": { |
| 279 | + "env": "production", |
| 280 | + "team": "backend" |
| 281 | + }, |
| 282 | + "annotations": { |
| 283 | + |
| 284 | + } |
| 285 | + } |
| 286 | + }' |
| 287 | +``` |
| 288 | + |
| 289 | +## Next Steps |
| 290 | + |
| 291 | +- Explore the [API Overview](api-overview.md) for detailed information about API conventions |
| 292 | +- Review the [Core Resources Guide](core-resources.md) for in-depth resource documentation |
| 293 | +- Check out the [Client SDK Guide](client-sdks.md) for generating language-specific clients |
| 294 | +- See [Authentication & Authorization](authentication.md) for advanced auth scenarios |
| 295 | + |
| 296 | +## Useful Tools |
| 297 | + |
| 298 | +- **CF CLI**: Official command-line tool - https://github.com/cloudfoundry/cli |
| 299 | +- **Postman Collection**: Import our OpenAPI spec into Postman for easy testing |
| 300 | +- **HTTPie**: User-friendly command-line HTTP client - https://httpie.io/ |
| 301 | + |
| 302 | +## Getting Help |
| 303 | + |
| 304 | +- Check the [Troubleshooting Guide](troubleshooting.md) for common issues |
| 305 | +- Visit the [Cloud Foundry Slack](https://cloudfoundry.slack.com) community |
| 306 | +- Review the official [CAPI Documentation](https://v3-apidocs.cloudfoundry.org/) |
0 commit comments