Example: Accessing a user's data

Here we'll go through an example on how to read a user's authorized information.

  1. Create your web application
  2. Register your app on Bratabase
  3. Set up a Connect with Bratabase button on your site that uses webflow authorization.
  4. Obtain and store an Authorization token from a user.
  5. Use this token to make authenticated requests to access authorized data.

Making authenticated requests

On this example we will use a fictitious token that grants access to all scopes and fake application credentials.

For this example I will be using the command line curl. If you have Mac or Linux you should already have this installed, if you are on Windows you may need to install it.

Start by visiting the API root, and we'll navigate from there:

{
    "self": "https://api.bratabase.com/", 
    "meta": {
        "welcome": <string>,
    }, 
    "links": {
        "me": "https://api.bratabase.com/me/", 
        "brands": <url>, 
        "rate_limit": <url>
    }, 
    "rel": "resource", 
    "spec": <url>
}

We are interested on the url under links.me, so we follow that.

$ curl -i https://api.bratabase.com/me/

And we obtain this response:

{
    "message": "Not authenticated", 
    "spec": "http://developers.bratabase.com/token-authentication/"
}

This means that we cannot access this resource without authenticating with an authorization token. This token will allow us to access the resources that its scopes allow to the associated user. On this example our token is TOKEN123 so we will send that as an Authorization header bearer token.

$ curl -i -H 'Authorization: bearer TOKEN123' https://api.bratabase.com/me/

And now we can get the expected response

{
    "body": {
        "username": <string>, 
        "bratabase_url": <url>, 
        "user_id": <int>, 
        "reputation": <int>, 
        "avatar": {
            "medium": <url>
        }, 
        "is_verified": <boolean>, 
        "email": <string>
    }, 
    "links": {
        "bras": "https://api.bratabase.com/me/bras/",
        "measurements": "https://api.bratabase.com/me/measurements/",
    }, 
    "self": "https://api.bratabase.com/me/", 
    "meta": {
        "collection": null
    }, 
    "rel": "entity", 
    "spec": "http://developers.bratabase.com/user-data/"
}

This response is the user's data which contains the information for the user of which the provided token belongs to.

Since this token has permissions to the [email scope[(http://developers.bratabase.com/access-scopes/#Email) the body of the response contains the email key with this user's email.

This token also has access to the user's bras and measurements this is why when we try to access those [collections] we will be able to see what is inside them.

Let's go inside and see how many bras and which does this user have (see how we are passing the "Authorization" header again).

$ curl -i -H 'Authorization: bearer TOKEN123' https://api.bratabase.com/me/bras/

And we will see the collection with all the bras this user has:

{
    "links": {}, 
    "self": "https://api.bratabase.com/me/bras/", 
    "collection": [
        {
            "bratabase_url": "http://www.bratabase.com/bra/abcde/", 
            "name": "28G - Freya \u00bb Active Molded Racerback Sports Bra (4891)", 
            "brand": {
                "bratabase_url": "http://www.bratabase.com/browse/freya/", 
                "href": "https://api.bratabase.com/brands/freya/",
                "name": "Freya"
            }, 
            "is_owned": true, 
            "href": "https://api.bratabase.com/me/bras/abcde/", 
            "model": {
                "bratabase_url": "https://www.bratabase.com/browse/freya/active-molded-racerback-sports-bra-4891/", 
                "href": "https://api.bratabase.com/brands/freya/models/active-molded-racerback-sports-bra-4891/",
                "name": "Active Molded Racerback Sports Bra (4891)"
            }, 
            "size": {
                "bratabase_url": "http://www.bratabase.com/browse/freya/active-molded-racerback-sports-bra-4891/28G/", 
               "href": "https://api.bratabase.com/brands/freya/models/active-molded-racerback-sports-bra-4891/sizes/28G/", 
                "size": "28G"
            }
        }
        ....  snip ....
    ], 
    "meta": {
        "page_total": null, 
        "paginated": false, 
        "next": null, 
        "current": null, 
        "collection_total": 17, 
        "prev": null
    }, 
    "rel": "collection", 
    "spec": null
}

Since this is a non paginated collection, we will get all bras at once. The number of entries can be read from the meta.collection_total value, which in this example tells us that this user has added 17 bras to the site. The specification of this response is according to the specified on the user's bras endpoint.

With this information we can already know how many bras a user has for each brand, and on each size.

Additionally if we want to know the measurements this user entered for a certain bra we can follow the href key for any of the entities and get a more detailed representation of this bra.

$ curl -i -H 'Authorization: bearer TOKEN123' https://api.bratabase.com/me/bras/abcde/

Which will get us this hefty response with all the bra's detail information.

{
    "body": {
        "bratabase_url": "http://www.bratabase.com/bra/dhx4j/", 
        "bust_perimeter": 0.0, 
        "name": "28G - Freya \u00bb Active Molded Racerback Sports Bra (4891)", 
        "hooks": 0, 
        "brand": "Freya",
        "strap_width": 0.0, 
        "cup_separation": 0.0, 
        "altered_band": false, 
        "is_owned": true, 
        "cup_depth": 0.0, 
        "is_altered": false, 
        "wire_length": 0.0, 
        "gore_height": 0.0, 
        "stretched_band_length": 0.0, 
        "model": "Active Molded Racerback Sports Bra (4891)",
        "band_length": 0.0, 
        "is_custom": false,
        "multiple_sizes": {
            "UK": "28G"
        }, 
        "wing_height": 0.0, 
        "size": "28G"
    }, 
    "links": {
        "size": "http://api.bratabase.com/brands/freya/models/active-molded-racerback-sports-bra-4891/sizes/28G/",
        "model": "https://api.bratabase.com/brands/freya/models/active-molded-racerback-sports-bra-4891/",
        "brand": "https://api.bratabase.com/brands/freya/"
    }, 
    "self": "https://api.bratabase.com/me/bras/dhx4j/", 
    "meta": {
        "collection": "https://api.bratabase.com/me/bras/"
    }, 
    "rel": "entity", 
    "spec": null
}

As you can see here, the body of the response matches the specification on the bra's detail page.

We can do the same procedure to access the user's measurements and see the detail for each of her entries.


Edit