👩‍💻Using your project

Learn how to make API requests to query your project's data

Now that we've deployed our project, and created an API key to authenticate ourselves, let's use our API and create some data.

Listing galaxies & planets

Although we don't have any data at the moment, let's list our galaxies to make sure everything is working as expected. Let's make a GET request to do so. You can use any HTTP client to do so, like curl, or Postman.

curl 'https://YOUR_PROJECT_URL/galaxies' \
--request GET \
--header 'x-api-key: YOUR_API_KEY'

If everything is working well, you should get an empty array in response:

[]

Let's do the same thing for planets:

curl 'https://YOUR_PROJECT_URL/planets' \
--request GET \
--header 'x-api-key: YOUR_API_KEY'

You should see the same result.

Creating a galaxy

Let's make a POST request to create our first galaxy.

curl 'https:///galaxies' \
--request POST \
--header 'x-api-key: ' \
--header 'content-type: application/json' \
--data '{"name": "Milky Way"}'

This should return a galaxy with an auto-generated id and a name set to "Milky Way", like this:

{
    "id": "d653f053-b07f-4ace-af00-d1d3da3af225", // randomly generated ID
    "name": "Milky Way"
}

You can try to run the first request to see that our galaxy is now present in the list of galaxies.

Creating a planet

We can also create a Planet; let's create planet Earth:

curl 'https://YOUR_PROJECT_URL/planets' \
--request POST \
--header 'x-api-key: YOUR_API_KEY' \
--header 'content-type: application/json' \
--data '{
  "name": "Earth",
  "mass": 5972,
  "habitable": true,
  "discovered_at": "0001-01-01T00:00:00.000Z"
}'

You should get a result with an automatically generated ID, which should look something like this:

{
    "id": "1a54f440-249f-4faf-a984-dd4864e2d161",
    "mass": 5972,
    "name": "Earth",
    "habitable": true,
    "atmosphere": [],
    "discovered_at": "0001-01-01T00:00:00.000Z",
    "galaxyId": null
}

Creating a planet linked to a galaxy

Now, let's say we want to create planet Neptune in our model, and have it be linked to the Milky Way galaxy we created just before. When we defined our Planet model, we added a property called galaxy, which represents a related Galaxy entity (using the the reference keyword).

{
  "Planet": {
    "primary_key": "id",
    "properties": {
      // other properties...
      "galaxy": {
        "type": "reference",
        "reference": "Galaxy"
      }
    }
  }
}

This allows us to link a Planet to a Galaxy, directly upon creation. By re-using the Galaxy id we obtained when we created the Milky Way, we can write the following request to create planet Neptune:

curl https://YOUR_PROJECT_URL/planets \
--request POST \
--header 'x-api-key: YOUR_API_KEY' \
--header 'content-type: application/json' \
--data '{
  "name": "Neptune",
  "mass": 1024,
  "habitable": false,
  "discovered_at": "1846-09-23T00:00:00.000Z",
  "galaxy": {
    "id": ""
  }
}'

The result should look something like this, with an automically generated id and a galaxyId set to the galaxy's id we passed as input.

{
    "id": "65f74f2d-4cb0-4135-926c-b82678608a62",
    "mass": 1024,
    "name": "Neptune",
    "habitable": true,
    "atmosphere": [],
    "discovered_at": "1846-09-23T00:00:00.000Z",
    "galaxyId": "GALAXY_ID"
}

Our API seems to work well, the last thing we can do is make some updates to our data model or API if we need to.

Last updated