Migrating from Postman¶
A Postman collection comes across in one command. apic import turns
folders into .http files, requests into named requests, environments into
http-client.env.json, auth helpers into # @auth, and the simple
pm.test and pm.environment.set lines into # @assert and # @capture.
What it cannot translate it lists, request by request, so nothing is
dropped silently.
How the concepts map¶
| Postman | apic |
|---|---|
| Collection | a project: a directory with .http files and apic.yaml |
| Folder | a .http file (todos.http; nested folders join with -) |
| Request | a ### block with # @name |
| Collection variables | $shared in http-client.env.json |
| Environment | an environment in http-client.env.json; secret values go to http-client.private.env.json |
{{var}}, :id path variables, {{$guid}} |
{{var}}, {{id}}, {{$uuid}} |
| Auth tab (bearer, basic, digest, AWS, API key, OAuth2 client credentials and password) | # @auth on the request; the collection's own auth becomes auth.default in apic.yaml |
pm.test with pm.response.to.have.status(200) |
# @assert status == 200 |
pm.expect(json.x).to.eql(1), .include, .exist, .be.true |
# @assert body.$.x == 1, contains, exists, == true |
pm.expect(pm.response.responseTime).to.be.below(500) |
# @assert duration < 500 |
pm.environment.set("token", json.token) |
# @capture token = body.$.token, kept in the session between runs |
| Collection runner | apic run file.http (a flow, in order) or apic run a b c |
| Runner with a data file | apic run --data rows.csv |
| Newman | apic run --json, --report report.html, or apic test --format junit for features |
| Pre-request script | not translated: see what does not carry over |
Import a collection¶
The collection below has what most real ones have: a log-in request whose
test stores the token, a request that uses it, a pre-request script and
a secret in the environment. It targets apic demo, so every step here
runs as written (start it with apic demo in another terminal).
cat > shop.postman_collection.json <<'EOF'
{
"info": {"name": "Shop", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"},
"variable": [{"key": "baseUrl", "value": "http://localhost:8089"}],
"item": [
{"name": "Auth", "item": [
{"name": "Log in",
"event": [{"listen": "test", "script": {"exec": [
"pm.test(\"logged in\", () => pm.response.to.have.status(200));",
"const body = pm.response.json();",
"pm.environment.set(\"token\", body.access_token);"
]}}],
"request": {"method": "POST", "url": "{{baseUrl}}/auth/login",
"header": [{"key": "Content-Type", "value": "application/json"}],
"body": {"mode": "raw", "raw": "{\"user\": \"alice\", \"password\": \"{{password}}\"}"}}}
]},
{"name": "Todos", "item": [
{"name": "List todos",
"event": [
{"listen": "prerequest", "script": {"exec": ["pm.variables.set(\"started\", Date.now());"]}},
{"listen": "test", "script": {"exec": [
"pm.test(\"ok\", () => pm.response.to.have.status(200));",
"pm.test(\"fast\", () => pm.expect(pm.response.responseTime).to.be.below(1000));"
]}}],
"request": {"method": "GET", "url": "{{baseUrl}}/todos",
"auth": {"type": "bearer", "bearer": [{"key": "token", "value": "{{token}}"}]}}}
]}
]
}
EOF
cat > local.postman_environment.json <<'EOF'
{"name": "local", "values": [{"key": "password", "value": "s3cret", "type": "secret", "enabled": true}]}
EOF
apic import shop.postman_collection.json -o shop --postman-env local.postman_environment.json --force
wrote shop/auth.http
wrote shop/todos.http
wrote shop/http-client.private.env.json
wrote shop/apic.yaml
wrote shop/http-client.env.json (baseUrl = http://localhost:8089)
2 request(s) generated; run `apic list` to see them
note list-todos: pre-request script: not translated; compute values with --var or a # @ref request instead
Each folder is a file, and the tests became directives:
# auth — generated by `apic import` from "shop.postman_collection.json". Edit freely.
### Log in
# @name log-in
# @capture token = body.$.access_token
# @assert status == 200
POST {{baseUrl}}/auth/login
Content-Type: application/json
{"user": "alice", "password": "{{password}}"}
# todos — generated by `apic import` from "shop.postman_collection.json". Edit freely.
### List todos
# @name list-todos
# @auth bearer {{token}}
# @assert status == 200
# @assert duration < 1000
GET {{baseUrl}}/todos
The password went to http-client.private.env.json, which belongs in
.gitignore; apic.yaml makes local the default environment.
Run it, and let the token look after itself¶
In Postman the runner went through the requests in order so the token was
set before it was used. apic keeps captured values in a session between
commands, so running log-in once is enough; better still, say that
list-todos needs it, and apic runs log-in first whenever the token is
missing:
cat > shop/todos.http <<'EOF'
# todos — generated by `apic import` from "shop.postman_collection.json". Edit freely.
### List todos
# @name list-todos
# @ref log-in
# @auth bearer {{token}}
# @assert status == 200
# @assert duration < 1000
GET {{baseUrl}}/todos
EOF
apic session clear -C shop
apic run list-todos -C shop | grep -E '^(POST|GET|✓|↳)'
POST http://localhost:8089/auth/login
✓ status == 200
↳ token = mock-token
↳ ran log-in first (# @ref)
GET http://localhost:8089/todos
✓ status == 200
✓ duration < 1000
apic run log-in list-todos -C shop still runs both in order, and
apic run todos.http -C shop runs a whole folder's worth as a flow.
A bigger example: Postman Echo¶
Postman's own Postman Echo collection, for the postman-echo.com service, is a good test of an import, since it uses query parameters, form bodies, basic and digest auth and plenty of tests. Fork it from Postman's public workspace, export it as Collection v2.1, then:
apic import "Postman Echo.postman_collection.json" -o echo
apic list -C echo
apic validate -C echo
apic run -C echo "$(ls echo/*.http | head -1 | xargs basename)" --keep-going
Read the note lines the import prints: they are the tests and scripts
that need a decision (see the next section). The requests themselves run
against the live service as they are.
What does not carry over¶
- Scripts. apic has no JavaScript, deliberately.
Every script line the importer does not recognise is printed as a
noteand listed underunsupportedin--json. Their usual jobs have homes: a computed value comes from--var,APIC_VAR_*or a built-in such as{{$timestamp}}; a token from# @author a# @refrequest; a check from# @assert; anything else from a shell script aroundapic run --json. - Browser OAuth2 flows, NTLM and Hawk. Reported by the importer. apic does OAuth2 client credentials, password, device code and authorization code with PKCE: see authentication.
- Mock servers, monitors, workspaces and sharing. Postman's hosted features have no equivalent; the project is files in git.
- Collection v1. Export as v2.1.
Checklist¶
-
apic import collection.json -o api --postman-env env.jsonfor each environment - Read every
noteline and decide:# @assert,# @capture,--var, or delete - Add
http-client.private.env.jsonand.apic/to.gitignore - Put
# @ref log-inon the requests that need the token -
apic validateis clean andapic fmt --checkpasses -
apic run <file>.http --jsonin CI in place of Newman
See also: apic import in the CLI reference, the
.http format, and lesson 9
of the course, which imports an OpenAPI document too.